[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/shift [Back]  [Original]

Array.prototype.shift() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.shift()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

shift()

In this article

const array1 = [1, 2, 3];

const firstElement = array1.shift();

console.log(array1);
// Expected output: Array [2, 3]

console.log(firstElement);
// Expected output: 1

js
arr.shift()

undefined

shift length 0 undefined

shift called applied length

myFish

js
var myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log("myFish before:", JSON.stringify(myFish));
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

var shifted = myFish.shift();

console.log("myFish after:", myFish);
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log("Removed this element:", shifted);
// Removed this element: angel

while shift()

shift() while

js
var names = ["Andrew", "Edward", "Paul", "Chris", "John"];

while ((i = names.shift()) !== undefined) {
  console.log(i);
}
// Andrew, Edward, Paul, Chris, John

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.shift


Web Proxy Viewer  |  New URL  |  Original Page