JS Built-ins
← Array207 / 226
ArrayArray.prototype.unshiftbeginner

Add Element to Start of Array

πŸ“– Usage Example
const arr = [2, 3, 4]
arr.unshift(1)  // returns 4 (new length)
arr  // β†’ [1, 2, 3, 4]

Use unshift() to prepend one element to the beginning of an array.

Array.prototype.unshift

unshift prepends one or more elements to the beginning of an array and returns the new length.

Implement addToStart which unshifts val onto arr and returns the new length.

πŸ’‘ Hints (2)
  • β€’ unshift mutates the array and returns the new length
  • β€’ Return the result of arr.unshift(val) directly