Python developer here. I am using JavaScript to run the following command,
let a = [1, 2, 3];
let b , c, d = a;
console.log(b);
console.log(c);
console.log(d);
To my surprise, b and c are undefined while d is the entire a variable. I later realize that I am meant to go let [b, c, d] = a;, however I am just trying to understand what is happening and why they’re undefined.
>Solution :
You are not destructuring.
let b , c, d = a;
Declares 3 variables and assigns a value to one of them. The line above is identical to:
let b;
let c;
let d = a;
This syntax allows you to declare (and optionally assign) several variables in a single statement, e.g.
let tmp, x = 4, i, y = 2, z = x * 10 + y;
Unassigned variables are default-initialized to undefined.