JS Built-ins
← Array162 / 226
ArrayArray.prototype.shiftbeginner

Array.prototype.shift() β€” remove and return the first element

πŸ“– Usage Example
const arr = [10, 20, 30]
const first = arr.shift()  // β†’ 10
arr  // β†’ [20, 30]

Use shift() to remove and return the first element of an array.

Array.prototype.shift()

shift() removes the first element from an array, mutates it, and returns the removed element.

const arr = [1, 2, 3]
const first = arr.shift() // first === 1, arr === [2, 3]
πŸ’‘ Hints (2)
  • β€’ `shift()` both removes the first element AND returns it.
  • β€’ You can directly `return arr.shift()`.