Q 1: Name two ways to dynamically add the value “bar” to the array “foo”
A:
1 2 3 |
foo.push("bar") foo[foo.length] = "bar"; |
Hint: Two Ways to Dynamically Append an Element to a JavaScript Arrayy
Q 2: Of what JavaScript type is an Array?
A: object
Hint: Associative Arrays in JavaScript
Q 3: What property tells you the length of a JavaScript array?
A: The “length” property
More Info on the JavaScript Array’s “length” property:
Q 4: If the array “foo” has a length of 10, what is the index of the first element in the array?
A: 0
Hint: JavaScript Arrays are zero-based
Q 5: If the array “foo” has a length of 10, what is the index of the last element in the array?
A: 9
Hint: JavaScript Arrays are zero-based
Q 6: What is the syntax you would use to assign the first element in the array “foo” to the variable “bar”?
A:
1 |
var bar = foo[0]; |
Q 7: True of False: An element in a JavaScript array can be another array
A: True
Hint: JavaScript Array elements can be arrays
Q 8: Given the following line of code, what is the length of the array “foo”?
1 |
var foo = []; |
A: 0
Hint: foo is an Array literal, but in this line of code, no elements have been defined.
Q 9: What does the array.shift() method do, and what does it return?
A: It removes the first element from the array and returns that element
More info on the JavaScript array.shift() method:
JavaScript Array Management with Push(), Pop(), Shift() and Unshift()/a>
Q 10: Given the following line of code, what would be the output of the console?
1 2 |
var foo = new Array(3); console.log(foo); |
A: [undefined, undefined, undefined]
Hint: When you instantiate the JavaScript Array() constructor, and pass in a single number, that number will indicate the length of the array, but the elements are still not initialized (i.e. they are all undefined)