Array.prototype.shift
Removes the first element from an array and returns that removed element, changing the length of the array
Syntax
array.shift()Return Value
The removed element, or undefined if the array is empty
Examples
const arr = [1, 2, 3];
const first = arr.shift();
console.log(first); // 1
console.log(arr); // [2, 3]const queue = ['first', 'second', 'third'];
while (queue.length) {
console.log(queue.shift());
}
// first, second, thirdconst empty: string[] = [];
console.log(empty.shift()); // undefinedUnderstanding 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
Array.prototype.unshiftAdds the specified elements to the beginning of an array and returns the new length of the array
Array.prototype.popRemoves the last element from an array and returns that element, changing the length of the array
Array.prototype.pushAdds the specified elements to the end of an array and returns the new length of the array
Array.prototype.spliceChanges the contents of an array by removing or replacing existing elements and/or adding new elements in place
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.