JavaScript Interview Questions: Arrays


Q 1: Name two ways to dynamically add the value “bar” to the array “foo”

A:

Hint: There are Two Ways to Dynamically Add an Element to a JavaScript Array


Q 2: Of what JavaScript type is an Array?

A: object

Hint: JavaScript arrays are Objects


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:

http://www.roseindia.net/java/javascript-array/javascript-array-length-function.shtml

http://www.bloggingdeveloper.com/post/JavaScript-Array-Length-Property.aspx


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:


Q 7: True of False: An element in a JavaScript array an 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”?

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:

http://www.w3schools.com/jsref/jsref_shift.asp

http://www.bennadel.com/blog/1796-Javascript-Array-Methods-Unshift-Shift-Push-And-Pop-.htm

http://www.devguru.com/technologies/ecmascript/quickref/shift.html

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/shift


Q 10: Given the following line of code, what would be the output of the console?

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)


Helpful Links for JavaScript Arrays

W3C Schools

http://www.w3schools.com/js/js_obj_array.asp

Mozilla Developer Network

https://developer.mozilla.org/en-US/search?q=javascript+array

JavaScriptKit

http://www.javascriptkit.com/jsref/arrays.shtm

JavaScript Mountain

http://javascriptmountain.com/?s=array