🔙

shift()

🎉
💨
Time to remove from the front! The engine is leaving!

The shift() Method

The shift() method removes the first item from an array and returns it.

It's like the engine departing, and all the other cars shift forward to take its place!

💡
💨

How shift() Works

1const letters = ["A", "B", "C", "D"];
2
3// Remove the first item
4const first = letters.shift();
5
6console.log(first); // "A"
7console.log(letters); // ["B", "C", "D"]
8
9// Notice: all indices shifted!
10// "B" is now at index 0
🤔
💨

Important: Indices Change!

When you use shift(), all remaining items move to the left. Their index numbers all decrease by 1!

Watch the index numbers change as you shift items!

🎮

Try It Yourself!

Click shift() to remove items from the front. Watch all the indices change!

[
A
[0]
B
[1]
C
[2]
D
[3]
]
length: 4