The shift()
method of Array
instances removes the first element from an array and returns that removed element. This method changes the length of the array.
The shift()
method of Array
instances removes the first element from an array and returns that removed element. This method changes the length of the array.
shift()
None.
The removed element from the array; undefined
if the array is empty.
The shift()
method removes the element at the zeroth index and shifts the values at consecutive indexes down, then returns the removed value. If the length
property is 0, undefined
is returned.
The pop()
method has similar behavior to shift()
, but applied to the last element in an array.
The shift()
method is a mutating method. It changes the length and the content of this
. In case you want the value of this
to be the same, but return a new array with the first element removed, you can use arr.slice(1)
instead.
The shift()
method is generic. It only expects the this
value to have a length
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.
The following code displays the myFish
array before and after removing its first element. It also displays the removed element:
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
The shift() method is often used in condition inside while loop. In the following example every iteration will remove the next element from an array, until it is empty:
const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"]; while (typeof (i = names.shift()) !== "undefined") { console.log(i); } // Andrew, Tyrone, Paul, Maria, Gayatri
The shift()
method reads the length
property of this
. If the normalized length is 0, length
is set to 0
again (whereas it may be negative or undefined
before). Otherwise, the property at 0
is returned, and the rest of the properties are shifted left by one. The length
property is decremented by one.
const arrayLike = { length: 3, unrelated: "foo", 2: 4, }; console.log(Array.prototype.shift.call(arrayLike)); // undefined, because it is an empty slot console.log(arrayLike); // { '1': 4, length: 2, unrelated: 'foo' } const plainObj = {}; // There's no length property, so the length is 0 Array.prototype.shift.call(plainObj); console.log(plainObj); // { length: 0 }
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
shift |
1 | 12 | 1 | 4 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1.0 | 0.10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift