Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.shift()

BaselineWidely available

Theshift() method ofArray instances removes thefirstelement from an array and returns that removed element. This method changes the lengthof the array.

Try it

const array1 = [1, 2, 3];const firstElement = array1.shift();console.log(array1);// Expected output: Array [2, 3]console.log(firstElement);// Expected output: 1

Syntax

js
shift()

Parameters

None.

Return value

The removed element from the array;undefined if the array is empty.

Description

Theshift() method shifts all values to the left by 1 and decrements the length by 1, resulting in the first element being removed. If thelength property is 0,undefined is returned.

Thepop() method has similar behavior toshift(), but applied to the last element in an array.

Theshift() method is amutating method. It changes the length and the content ofthis. In case you want the value ofthis to be the same, but return a new array with the first element removed, you can usearr.slice(1) instead.

Theshift() method isgeneric. It only expects thethis value to have alength property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

Examples

Removing an element from an array

The following code displays themyFish array before and after removing itsfirst element. It also displays the removed element:

js
const myFish = ["angel", "clown", "mandarin", "surgeon"];console.log("myFish before:", myFish);// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']const shifted = myFish.shift();console.log("myFish after:", myFish);// myFish after: ['clown', 'mandarin', 'surgeon']console.log("Removed this element:", shifted);// Removed this element: angel

Using shift() method in while loop

The shift() method is often used in condition inside while loop. In the followingexample every iteration will remove the next element from an array, until it is empty:

js
const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"];while (typeof (i = names.shift()) !== "undefined") {  console.log(i);}// Andrew, Tyrone, Paul, Maria, Gayatri

Calling shift() on non-array objects

Theshift() method reads thelength property ofthis. If thenormalized length is 0,length is set to0 again (whereas it may be negative orundefined before). Otherwise, the property at0 is returned, and the rest of the properties are shifted left by one. The property atlength - 1 isdeleted, and thelength property is decremented by one.

js
const arrayLike = {  length: 3,  unrelated: "foo",  2: 4,};console.log(Array.prototype.shift.call(arrayLike));// undefined, because it is an empty slotconsole.log(arrayLike);// { '1': 4, length: 2, unrelated: 'foo' }const plainObj = {};// There's no length property, so the length is 0Array.prototype.shift.call(plainObj);console.log(plainObj);// { length: 0 }

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.shift

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp