JS-056

Beginner4 min~4 min read#array#shift#arrays
shift()
Remove from the beginning of an array
Goal
Learn to remove the first element of an array using shift().
Explanation
- shift() removes the first element of an array.
- After it is removed, all the other elements shift to the left.
- The method changes the original array.
- It works with the beginning of an array.
- shift() is useful when working with queues.
Analogy
Imagine a line at a store. shift() removes the person standing first in line.
Code
const fruits = [
"Apple",
"Banana",
"Orange"
];
fruits.shift();
console.log(fruits);Result: ["Banana", "Orange"]
Common mistake
Beginners confuse shift() with pop(). shift() works with the beginning of an array, while pop() works with the end.
Remember
- ✦ shift() removes the first element.
- ✦ It works with the beginning of an array.
- ✦ It changes the array.
Quick Quiz
What does shift() do to ["Apple", "Banana", "Orange"]?