配列の最初の値を取り出す Array.shift

配列公式データ取得日:2026年11月13日動作確認待ち

ひとことで言うと

配列から最初の要素を削除し、その値を返します。このメソッドは、配列の長さを変更します。

ブロックとコード

ブロックの文言は公式の日本語版に合わせています(形は簡易表示です)。

使用例

If you have an array with 4 numbers, like 1, 2, 3, and 4, you remove the first number from the array by shifting it from the front. To shift the number from the array in blocks, do this:

JavaScript

let thoseNumbers = [1, 2, 3, 4];
let firstNumber = thoseNumbers.shift();

Python

thoseNumbers = [1, 2, 3, 4]
firstNumber = thoseNumbers.shift()

Find out how many odd numbers there are as you remove all the numbers from a list.

JavaScript

let odds = 0;
let ints = [45, 78, 98, 3, 5, 6, 12, 643, 0, 34, 4];
while (ints.length > 0) {
    if ((ints.shift() % 2) > 0) {
        odds++;
    }
}

Python

odds = 0
ints = [45, 78, 98, 3, 5, 6, 12, 643, 0, 34, 4]
while len(ints) > 0:
    if (ints.shift() % 2) > 0:
        odds += 1
執筆:鈴木孝昌官公庁のWeb・システム開発を担当する現役プロジェクトマネージャー。マイクラカップ沖縄代表チームを指導(第6回・第7回、第7回TBS賞)。
沖縄マイクラ部で、子どもたちにMakeCodeを教えています。プロフィールと編集方針
Instagramで教室の様子を見る
noteで記事を読む