DEV Community

Maria Boldyreva
Maria Boldyreva

Posted on

JavaScript: getting the first element of an array without indexes

const a = [1, 2, 3];
const [b] = a;
b
// 1

[] is the decomposing operator.

Also:

const [c, ...rest] = a;
c
// 1
rest
// [2, 3]

... is the rest operator.

Top comments (2)

Collapse
 
hdennen profile image
Harry Dennen

Short and sweet, I like it.

Collapse
 
michelc profile image
Michel

I like it. This is exactly what I was looking for.