- Instant help with your JavaScript coding problems

Get the first element of an array in JavaScript

Question:
How to get the first element of an array in JavaScript?
Answer:
const firstItem = myArray.shift();
Description:

To get the first element of an array in JavaScript you can use the shift() method on your array. The shift() method removes the item from the original array.

For example if you have the following array:

let myArray = [1, 2, 3, 4, 5];

Then the following code gets the first item:

const firstItem = myArray.shift();

After that the values are:

// firstItem: 1
// myArray: [2, 3, 4, 5]
Share "How to get the first element of an array in JavaScript?"