EN
JavaScript - shift array elements
0
points
In this article, we would like to show you how to shift array elements in JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
const letters = ['A', 'B', 'C', 'D'];
letters.shift(); // removes 'A' from letters array
console.log(letters);
Practical example
The shift() method removes the first element from an array and returns that removed element, so you can use it later.
// ONLINE-RUNNER:browser;
const letters = ['A', 'B', 'C', 'D'];
const shifted = letters.shift(); // shifts 'A' from letters array to shifted const
console.log(letters); // B,C,D
console.log(shifted); // A
Output:
B,C,D
A