APIs Flashcards
What does “[1,2].concat(3,[4,,5])” result in?
A new array of “[1,2,3,4,,5]”, as concat flattens arguments that are arrays.
What does “[1,2,3].slice(-2, -1)” result in?
A new array of “[2]”
What does “[1,2,3].slice(-2)” result in?
A new array of “[2, 3]”
When is an array property name an index to an element?
If “ToString(ToUint32(P))” is equal to “P” and “ToUint32(P)” is not equal to (2^32)−1. (i.e. an integer where 0 < MaxUInt32).
What is the maximum length of an array?
(2^32) - 1. (i.e. MaxUInt32)
What is the behavior of “Array” when called as a function or a constructor?
The same in either case. When called with a single number argument, returns a new Array with that many elements, else returns a new Array with the initial elements set to the arguments given.
What does “new Array(-5)” return?
It throws a RangeError exception.
How can you test if an object is an Array?
“Array.isArray(obj)”. This tests if the “[[class]]” internal property is ‘Array’, so it is not true for ‘array like’ objects.
What do “[,’a’,’b’].join(‘-‘)” and “[0,1].join()” return?
“-a-b” and “0,1” respectively.
What does “[].pop()” return?
undefined
How does Array.prototype.pop work?
It returns the (length-1) element from the array (this) and removes it from the array.
How does Array.prototype.push work?
Adds the arguments given to the end of the array (this), and returns the new length.
How does Array.prototype.reverse work?
It reverses the elements of the array in place, and returns a reference to the array.
How would you get the first element from an array ‘arr’ and remove it from the array (sliding other elements down by 1)?
“arr.shift()”
If arr = [‘a’,’b’,’c’,’d’], what does “arr.slice(-2, -1)” do?
Returns a new array [‘c’]. The original array is unchanged