These questions ask you to follow array indexes, sparse length, reads, insertion and removal methods, copying, and destructuring. Keep three notes as you work: which positions are present, whether the source changes, and what value the expression returns.
Model the positions before the method
Ordinary array positions begin at zero. For a nonempty array, length is normally one more than the highest present index unless length was explicitly enlarged. Assigning beyond the current end raises length to the assigned index plus one, while skipped positions are holes rather than automatically stored null or undefined values. Reading either a hole or an out-of-range position produces undefined, but that result alone cannot prove the position is absent because an element may explicitly hold undefined.
| Operation | Mutation and return procedure |
|---|---|
| push and pop | push adds at the end and returns the new length; pop removes and returns the last element. If [] receives pop, the result is undefined. |
| shift and unshift | shift removes and returns the first element; unshift adds at the beginning and returns the new length. All four end methods mutate their array. |
| slice | Select a range into a new array without changing the source. ["p", "q", "r"].slice(0, 2) creates ["p", "q"]. |
| splice | Insert, replace, or remove inside the source, then return an array of removed elements. Record the source and returned array separately. |
| Array.isArray | Use Array.isArray(value) to distinguish arrays from ordinary non-array objects. typeof labels both as object, and a length property proves nothing. |
| spread and destructuring | Spread creates a new outer array but reuses nested object references. A pattern such as [head, , tail] reads by position, skips one binding, and does not remove anything from the source. |
The tempting wrong routes
- Starting ordinary positions at one shifts every access. Translate human first and second into indexes zero and one before reading the array.
- Treating a missing read as an exception or null applies a bounds policy JavaScript arrays do not use. The read produces undefined, with no automatic nearest-element fallback.
- Expecting insertion methods to return the inserted value or updated array confuses mutation with the expression result. push and unshift report the new length.
- Swapping slice and splice reverses both mutation and return reasoning. The short name without p makes a new range; the form with p changes the source and reports removals.
- Treating a skipped destructuring position as deletion changes the source in your mental model. The pattern only decides which positions receive bindings.
Try it
Starting from an empty array, values are assigned only at indexes 0 and 4, and no other length-changing operation occurs. What is its length?
- 2
- 5
- 4
- undefined
Which statements about Array splice are true? Select all that apply.
- It never changes its source array
- It always returns the final array length
- It can insert or remove elements
- It returns an array of removed elements
A shallow array copy is made with spread from an array containing a nested object. What is shared between source and copy?
- The nested object reference
- The outer array identity
- Nothing at any depth
- Only the length property object