Array.prototype.shift()
Removes the first element from an array and returns it.
Syntax
arr.shift() Returns
any — The removed first element, or undefined if the array is empty.
Examples
const a = [1, 2, 3];
console.log(a.shift(), a);
Output
1 [ 2, 3 ]
console.log([].shift());
Output
undefined
Notes
Mutates the array in place and re-indexes remaining elements (O(n)). Returns
undefined for an empty array. Use `pop` to remove from the end.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |