Home > Software design >  Default behavior of destructuring assignment without brackets
Default behavior of destructuring assignment without brackets

Time:10-09

What happens when the brackets are left off on the lhs when using the destructing 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?

CodePudding user response:

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];

CodePudding user response:

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]
  • Related