Array

Array.prototype.shift

Removes the first element from an array and returns that removed element, changing the length of the array

Syntax

JavaScript
array.shift()

Return Value

The removed element, or undefined if the array is empty

Examples

Basic Usage
const arr = [1, 2, 3];
const first = arr.shift();
console.log(first); // 1
console.log(arr); // [2, 3]
Practical Example
const queue = ['first', 'second', 'third'];
while (queue.length) {
  console.log(queue.shift());
}
// first, second, third
Advanced Usage
const empty: string[] = [];
console.log(empty.shift()); // undefined

Understanding Array.prototype.shift

The Array.prototype.shift method in JavaScript removes the first element from an array and returns that removed element, changing the length of the array. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.

The method signature is array.shift(). When called, it returns the removed element, or undefined if the array is empty. Understanding when and how to use shift() helps you write more expressive, readable code.

Common use cases for Array.prototype.shift include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-unshift, array-pop, array-push, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Array.prototype.shift is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Array Methods

Other methods in the Array object

Related Tools

More Array Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.