Get first item list in Javascript
A simple and fairly efficient solution to fetch the first element of an array in JavaScript is using the [] operator. This method is demonstrated below:
var arr = [ 1, 2, 3, 4, 5 ];
var first = arr[0];
console.log(first);
Output
1
The shift() method returns the first element from an array but removes it from the array as well. To avoid modifying the original array, you can create a copy of the array before calling the shift() method. You can do this in two ways:
var arr = [ 1, 2, 3, 4, 5 ];
var first = arr.slice(0, 1).shift();
console.log(first);
Output
1