Quasilinear Thoughts

- A developer's log book.

Published on

How to get the first element of JavaScript array without using index

Authors

How to get the first element of JavaScript array without using index

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..!

Comments are closed but If you want to respond, please send me a message over WhatsApp or facebook or tweet or send email.

Khalil

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.