JS Built-ins
← Array210 / 226
ArrayArray.prototype.unshiftintermediate

unshift Mutates the Array

πŸ“– Usage Example
const arr = ['b', 'c']
arr.unshift('a')
console.log(arr)  // β†’ ['a', 'b', 'c']  (mutated)

unshift() mutates the original array β€” use toSpliced(0, 0, el) for an immutable alternative.

Array.prototype.unshift β€” Mutation

unshift modifies the original array in place. After calling it, the prepended value becomes arr[0].

Implement prependAndCheck which unshifts val and returns arr[0] to confirm it is now the first element.

πŸ’‘ Hints (2)
  • β€’ Call arr.unshift(val) first, then read arr[0]
  • β€’ After unshift, arr[0] is always the value you prepended