Languages
[Edit]
EN

JavaScript - shift array elements

0 points
Created by:
Rogan-Wilkes
337

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
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join