[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/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

20157

shift() Array

const array = [1, 2, 3];

const firstElement = array.shift();

console.log(array);
// : Array [2, 3]

console.log(firstElement);
// : 1

js
shift()

undefined

shift() 1 1 length 0 undefined

pop() shift()

shift() this this arr.slice(1)

shift() this length

myFish

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

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

const shifted = myFish.shift();

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

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

shift() while

shift() while

js
const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"];

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

shift()

shift() this length 0 length 0 undefined 0 1 length - 1 length 1

js
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.shift.call(arrayLike));
// undefined
console.log(arrayLike);
// { '1': 4, length: 2, unrelated: 'foo' }

const plainObj = {};
// length  length  0
Array.prototype.shift.call(plainObj);
console.log(plainObj);
// { length: 0 }

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.shift


Web Proxy Viewer  |  New URL  |  Original Page