Search code examples
javascriptecmascript-6destructuring

Pitfalls of destructuring in ES6


I was going through https://exploringjs.com/es6/ch_destructuring.html#sec_pitfalls-destructuring. The second point at this link says that "During destructuring, you can either declare variables or assign to them, but not both.". However, I have seen many examples where declaration and assignment is taking place simultaneously during destructuring. One of them is mentioned below:

const [x, y, z] = ['a', 'b', 'c'];
console.log(x, y, z); // prints a b c

In the example above, both declaration and assignment are happening. x, y, z are getting declared as constants and also getting a value assigned.

Could anybody help in understanding this statement? May be I am understanding it in a wrong manner.


Solution

  • I think meaning here is that if you have already defined variables you can't perform this:

    let a, b;
    const [a, b, c] = ['a', 'b', 'c'];