Array.prototype.shift
Removes the first element of an array and returns it.
Available since: ES3
Syntax
array.shift()Return value
anyThe removed first element, or undefined if the array is empty.
Examples
A queue: first in, first out:
const queue = ['Anna', 'Oleg', 'Ivan'];
const next = queue.shift();
console.log(next);
console.log(queue);Output:
Anna
[
"Oleg",
"Ivan"
]Notes
The other elements move one position to the left. On very large arrays this is slower than pop().
See also
- Array.prototype.unshift — Adds elements to the beginning of an array.
- Array.prototype.pop — Removes the last element of an array and returns it.
- Array.prototype.push — Appends elements to the end of an array.
In the tutorial
- Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.