carl.hiass
carl.hiass

Reputation: 1764

Default behavior of destructuring assignment without brackets

What happens when the brackets are left off on the LHS (Left Hand Side) when using the destructuring assignment syntax? For example:

{
    let a,b = [1,2];
    console.log(a, b);
}
{
    let [a,b] = [1,2];
    console.log(a, b);
}

If brackets are not included, does it act equivalent to doing:

let a=undefined, b=[1,2];

Or what exactly occurs when brackets are left off?

Upvotes: 0

Views: 61

Answers (2)

Audwin Oyong
Audwin Oyong

Reputation: 2556

When the brackets are left off on the LHS (Left Hand Side), it would become a variable assignment as you can declare multiple variables separated by a comma.

So:

let a,b = [1,2];

Is equivalent to:

let a;
let b = [1,2];

console.log(a); // undefined
console.log(b); // [1, 2]

Upvotes: 1

ray
ray

Reputation: 27245

You can declare multiple variables with a single let by separating them with a comma. So without the brackets you're effectively doing:

let a;
let b = [1, 2];

Upvotes: 2

Related Questions