DEV Community

Maria Boldyreva
Maria Boldyreva

Posted on

9 5

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.

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more