1

I'm running the following code:

const [a, b] = [1, 2]
const [c, d] = [3,4]
[a,b] = [b, a]
console.log(a, b, c, d)

Expected Result:

2,1,3,4

Actual Result:

1,2,2,1

I understand that putting semicolons at the end of each line will fix the problem, but I don't understand why I'm getting the result that I get, could someone explain why this happens?

everybody0523
  • 140
  • 1
  • 8
  • Why doing this `[a,b] = [b, a]`? – brk Aug 09 '19 at 04:12
  • If you don't want to use semicolons you **must** follow the standard.js rule on semicolons: https://standardjs.com/rules.html#semicolons - basically never start a line with `[` or `(` or `\`` – slebetman Aug 09 '19 at 04:26
  • I think the last paragraph makes this not a duplicate. I also wanted to know why the result came out the way it did even given that semicolons would make things different – pwilcox Aug 09 '19 at 04:27
  • 1
    @pwilcox Why doesn't the duplicate answer answer your question? It's literally the first sentence of the answer: `you should know which statements are affected by the automatic semicolon insertion` – slebetman Aug 09 '19 at 04:28
  • From the canonical question: `When, as the program is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true: The offending token is separated from the previous token by at least one LineTerminator.`. Here, `const [c, d] = [3,4] [a,b] = [b, a]` is a valid standalone statement, so no semicolon is inserted. – CertainPerformance Aug 09 '19 at 04:30
  • @slebetman, imagine I didn't want the semicolons. Imagine I ran into this code as one line. I would love to know why it outputs that way. The suggested question doesn't speak to that. – pwilcox Aug 09 '19 at 04:33
  • @CertainPerformance, the fact that he posted his code on different lines is a red herring, and perhaps the fact that the question was posed that way is unfortunate. But the last paragraph shows the intent. The fact that `const [c, d] = [3,4] [a,b] = [b, a]`caused the output described by the OP is what's really the core question. Adasskos's answer is what satisfies it, and it is not addressed by considering semicolon insertion. – pwilcox Aug 09 '19 at 05:02

3 Answers3

5

You cannot assign to [a, b] because it's const. What you're really doing is:

const [c, d] = [3,4][a,b] = [b, a]

since a = 1 and b = 2, this means

const [c, d] = [3,4][1,2] = [2, 1]

and

[3,4][1,2] = [2,1]

evaluates to simply [2,1] therefore:

a = 1 b = 2 c = 2 d = 1

it also sets 2nd element of array [3,4] to [2,1] but this array is not assigned anywhere so it doesn't matter

Adassko
  • 4,444
  • 16
  • 35
2

Semicolons matter:

let [a, b] = [1, 2];
let [c, d] = [3,4];
[a,b] = [b, a];
console.log(a, b, c, d);

In your original code, the second and third lines are interpreted together as const [c, d] = [3,4][a,b] = [b, a], resulting in 2 and 1 being assigned to c and d.

After adding the semicolons, an error will raised about assignment to a constant variable. Changing the const declarations to let gives you the expected output.

Robby Cornelissen
  • 72,308
  • 17
  • 104
  • 121
1

You might want to use var or let instead of const. You're basically doing the following as you haven't used the semicolons. a,b are being assigned to c,d:

const [a, b] = [1, 2]
const [c, d] = [3,4][a,b] = [b, a]
console.log(a, b, c, d)

While you might want to do:

var [a, b] = [1, 2];
var [c, d] = [3,4];
[a,b] = [b, a];
console.log(a, b, c, d);
shrys
  • 5,381
  • 2
  • 15
  • 29
  • please help me understand, why the downvote – shrys Aug 09 '19 at 04:16
  • no idea, this actually explains the result and doesn't presume to fix the code (let OP fix own code, which is a better learning experience, and he'll find the other issue then) – Jaromanda X Aug 09 '19 at 04:19
  • @JaromandaX got it, thanks – shrys Aug 09 '19 at 04:20
  • 1
    I was actually saying your answer was GOOD as it was in my opinion ... the OP asks why the code produces the results it does - and you provided an answer – Jaromanda X Aug 09 '19 at 04:22
  • @shrys I don't know the actual reason for the downvote but perhaps it's because `var` is not the problem here so the first statement is at best misleading and at worst completely wrong. See Adassko's answer that shows that `const` works. Indeed, the OP's question itself shows that const works. The problem is that automatic semicolon insertion doesn't work how the OP thinks it does (or the OP isn't aware that javascript's no-semicolon feature is implemented via automatic semicolon insertion) – slebetman Aug 09 '19 at 04:31
  • @slebetman that is a very valid point. Thank you for pointing that out, I'll keep that in mind :) – shrys Aug 09 '19 at 04:33
  • Great to see a use case where *const* gets in the way. Its only real purpose in life is to help programers, in this case it's a hindrance. :-) I've yet to see a good reason to stop using *var*, though `let a = 'foo'` is more semantic, but then I have to worry about scope… :-) – RobG Aug 09 '19 at 04:33
  • @RobG I'm sorry to say this is not one such use-case – slebetman Aug 09 '19 at 04:34
  • @RobG very insightful, I did learn a lot with this answer – shrys Aug 09 '19 at 04:44