13

Can anyone explain, why the following happens with ES6 array destructuring?

let a, b, c
[a, b] = ['A', 'B']
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

Expected: a=A b=BB c=C

Actual: a=BB b=C c=undefined

5
  • 5
    put ; after each line Commented Jun 27, 2016 at 9:26
  • yup, tested that, @PranavCBalan's comment works Commented Jun 27, 2016 at 9:28
  • 3
    This is why ASI is a misfeature. Commented Jun 27, 2016 at 9:31
  • True, @PranavCBalan's solution works. Wow, for me this is first case where the semicolons really matter in js code. I prefer semicolon-less code for cleaner syntax, but maybe I have to start to use them. Can anyone explain, why in this case the semicolons are really needed? Commented Jun 27, 2016 at 9:36
  • 2
    @ronkot Almost any time you start a line with array indexer or parenthesis you're going to have an issue (Example). Most devs who prefer semi-colonless start the line with a ; to fix ASI related issues. Commented Jun 27, 2016 at 10:52

4 Answers 4

16

As others have said, you're missing semicolons. But…

Can anyone explain?

There are no semicolons automatically inserted between your lines to separate the "two" statements, because it is valid as a single statement. It is parsed (and evaluated) as

let a = undefined, b = undefined, c = undefined;
[a, b] = (['A', 'B']
[(b, c)] = ['BB', 'C']);
console.log(`a=${a} b=${b} c=${c}`);

wherein

  • [a, b] = …; is a destructuring assignment as expected
  • (… = ['BB', 'C']) is an assignment expression assigning the array to the left hand side, and evaluating to the array
  • ['A', 'B'][…] is a property reference on an array literal
  • (b, c) is using the comma operator, evaluating to c (which is undefined)

If you want to omit semicolons and let them be automatically inserted where ever possible needed, you will need to put one at the start of every line that begins with (, [, /, +, - or `.

Sign up to request clarification or add additional context in comments.

8 Comments

Why did it turn [b, c] to a [(b, c)] actually?
Wow, this is the real answer for the question.... I were thinking what happening there... Now it's clear....
@zerkms: Because I like introducing grouping operators… in this case to emphasise that it is not an array literal with two elements
is ['A', 'B'][b, c] will get property b and c of array elements ?
After testing with several cases, Ithink ['A', 'B'][b, c] ==> ['A', 'B'][c]
|
7

You've fallen into a trap of line wrapping and automatic semicolon insertion rules in JavaScript.

Take this example:

let x = [1, 2]
[2, 1]

It's the interpreted as:

let x = [1, 2][2, 1] // === [1, 2][(2, 1)] === [1, 2][1] === 2

That weird [(2, 1)] thing above is related to how Comma Operator works.

Thus, your example:

let a, b, c
[a, b] = ['A', 'B']
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

Is interpreted as:

let a, b, c
[a, b] = ['A', 'B'][b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

Now, if you insert a semicolon, it will work as you intended:

let a, b, c
[a, b] = ['A', 'B']; // note a semicolon here
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

Also, it's a good idea to check your code by pasting it into Babel repl to see the generated output:

'use strict';

var a = void 0,
    b = void 0,
    c = void 0;

var _ref = ['A', 'B'][(b, c)] = ['BB', 'C'];

a = _ref[0];
b = _ref[1];

console.log('a=' + a + ' b=' + b + ' c=' + c);

Comments

2

I believe you have forgotten the line breaks ';'. Below is the corrected code. Please try:

let a,b,c
[a, b] = ['A', 'B'];
[b, c] = ['BB', 'C'];
console.log(`a=${a} b=${b} c=${c}`)

3 Comments

It would be helpful if you explained why ; is necessary there.
I believe the following expression will shed more light: let [a, b] = ['A', 'B'],[b, c] = ['BB', 'C'];
It would, if you additionally explain why it is syntactically correct and its semantics.
-1
let a, b, c
[a, b] = ['A', 'B']***;***
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

console: a=A b=BB c=C

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.