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

Prepend Multiple Elements

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

Pass multiple arguments to unshift() to prepend several elements at once.

Array.prototype.unshift β€” Multiple Elements

unshift accepts multiple arguments and inserts them all at the beginning in the order they are provided.

Implement prependAll which unshifts all vals onto arr and returns the new length.

πŸ’‘ Hints (2)
  • β€’ Spread the rest parameter: arr.unshift(...vals)
  • β€’ Elements are prepended in the order they appear in the argument list