Java Script: Arrays & Objects
Java Script: Arrays & Objects
Java Script: Arrays & Objects
console.log(myArray.length); //3
‘banana’]; console.log(a[4]);
// undefined
console.log(a[-9]); //
undefined
‘banana’]; console.log(a[4]);
// undefined
console.log(a[-9]); //
undefined
‘banana’]; console.log(a[4]);
// undefined
console.log(a[-9]); //
undefined
‘banana’]; console.log(a[4]);
// undefined
console.log(a[-9]); //
undefined
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana", undefined × 1, "panda", -5: "elephant"]
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana", undefined × 1, "panda", -5: "elephant"]
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana", undefined × 1, "panda", -5: "elephant"]
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana", undefined × 1, "panda", -5: "elephant"]
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana", undefined × 1, "panda", -5: "elephant"]
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana", undefined × 1, "panda", -5: "elephant"]
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana", undefined × 1, "panda", -5: "elephant"]
a[-5] = ‘elephant’;
console.log(a[-5]); // “elephant”
console.log(a);
// (5) ["cat", "dog", "banana",
undefined × 1, "panda", -5:
"elephant"]
SD4x-2 Property of Penn Engineering, Chris Murphy
Adding to an Array
• Elements can be added to arrays using push() and
unshift()
• push() will add elements to the end of the array
• unshift() will add elements to the beginning of the array
myArray.push(‘scooter’);
console.log(myArray); // car,bike,scooter
myArray.unshift(‘train’);
console.log(myArray); // train,car,bike,scooter
myArray.push(‘scooter’);
console.log(myArray); // car,bike,scooter
myArray.unshift(‘train’);
console.log(myArray); // train,car,bike,scooter
myArray.push(‘scooter’);
console.log(myArray); // car,bike,scooter
myArray.unshift(‘train’);
console.log(myArray); // train,car,bike,scooter
myArray.push(‘scooter’);
console.log(myArray); // car,bike,scooter
myArray.unshift(‘train’);
console.log(myArray); // train,car,bike,scooter
var pet = {
name: ‘Cooper’,
type: ‘dog’
}
console.log(pet.age); // undefined
pet.age = 11;
console.log(pet.age); // 11
var pet = {
name: ‘Cooper’,
type: ‘dog’
}
console.log(pet.age); // undefined
pet.age = 11;
console.log(pet.age); // 11
var pet = {
name: ‘Cooper’,
type: ‘dog’
}
console.log(pet.age); // undefined
pet.age = 11;
console.log(pet.age); // 11
var pet = {
name: ‘Cooper’,
type: ‘dog’
}
console.log(pet.age); // undefined
pet.age = 11;
console.log(pet.age); // 11
var pet = {
name: ‘Cooper’,
type: ‘dog’
}
console.log(pet.age); // undefined
pet.age = 11;
console.log(pet.age); // 11
var pet = {
name: ‘Cooper’,
type: ‘dog’
}
console.log(pet.age); // undefined
pet.age = 11;
console.log(pet.age); // 11
var pet = {
name: ‘Cooper’,
type: ‘dog’
}
console.log(pet.age); // undefined
pet.age = 11;
console.log(pet.age); // 11