Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the output 1? Can someone explain me step by step about what is happening in the second line here?

const a=[1,2,3,4,5];
const [n]=a;
console.log(n);

I need to know how does something inside square brackets after const means, and why it stored only 1 and not the entire array.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It is ES6 Destructuring Assignment.

const a = [1,2,3,4,5];
const [n] = a;
const [m, o] = a;
const [p, q, r] = a;
const [...s] = a;
const [t, u] = a;
console.log(n); // 1
console.log(m, o); // 1 2
console.log(p, q, r); // 1 2 3
console.log(...s); // 1 2 3 4 5
console.log([...s]); // [1, 2, 3, 4, 5]
console.log(u); // 2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading