DEV Community

Md Abdullah al noor
Md Abdullah al noor

Posted on

Answer: How to get the first element of an array?

ES6 Spread operator + .shift() solution

Using myArray.shift() you can get the 1st element of the array, but .shift() will modify the original array, so to avoid this, first you can create a copy of the array with [...myArray] and then apply the .shift() to this copy:

var

Top comments (2)

Collapse
 
cipharius profile image
Valts Liepiņš

What about keeping it simple as suggested in the accepted answer?

stackoverflow.com/a/4090513

Collapse
 
abdullahalnoor profile image
Md Abdullah al noor

Your right on your statement ..but I think when I handle State like in react/vue. It's handy & depend on situation....