Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

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

In the tutorial

  • Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.

Thanks for reading!

Donate to the site — we work hard for you.