- Published on
How to get the first element of JavaScript array without using index
- Authors
- Name
- Khalil
- @Im_Khalil
A simple and fairly efficient solution to fetch first element of array in JavaScript is by using [] operator.
Run the below code in the browser console.
var a = [1,2,3,4,5];
var [b] = a;
console.log(b);
How to get rest of the array elements: Just run the below snippet in browser console.
var [c,...rest] = a;
console.log("remaining array elements "+rest);
Cheers..!
Khalil Ganiga
Just another programmer.. This blog expresses my views of various technologies and scenarios I have come across in realtime.
Keep watching this space for more updates.