2

I swap variable using this way:

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

Why this way only works with semicolon at the end? Meanwhile JS works normally without semicolon.

  • 4
    In this case js wont be able to understand the termination of the code – brk Jun 21 '21 at 06:21
  • Can you explain for more detail? – Khanh Nguyen Jun 21 '21 at 06:23
  • 2
    JS has something called ASI (automatic semicolon insertion) which isn't really [a feature but an error facility to fix the places where semi-colons haven't been inserted](https://brendaneich.com/2012/04/the-infernal-semicolon/). That article is from the guy who invented JavaScript if you're unsure of its provenance. – Andy Jun 21 '21 at 06:26
  • You can't reference `b` as value for `a` because it doesn't exist yet. – rubenpoppe Jun 21 '21 at 06:36
  • Sorry, i edited my question – Khanh Nguyen Jun 21 '21 at 06:41
  • 1
    Does this answer your question? [No semicolon before \[\] is causing error in JavaScript](https://stackoverflow.com/questions/16664824/no-semicolon-before-is-causing-error-in-javascript) – Lionel Rowe Jun 21 '21 at 06:46

2 Answers2

3

This is because this part of the code:

let b = a
[a, b] = [b, a]

Is interpreted as:

let b = a[a, b] = [b, a]

Which means:

let b;          // Create variable b.

b = a[b];       // Assign to b the value of property b of a
                // for example if the value of b is "hello"
                // then b = a.hello. If the value of b is 0
                // then b = a[0];

a[b] = [b, a];  // Assign the array containing b and a to
                // property b of a. For example if the
                // value of b is "hello" then
                // a.hello = ["hello", a]

b = a[b];       // Assign the property b of a to b. For example
                // if the value of b is originally "hello" then
                // b = a.hello

Why is this? It's because of how line endings work in javascript. The algorithm is called automatic semicolon insertion. In javascript the end of line is not the end of a statement. This allows us to write multi-line statements. However if the end of line happens to be the end of a statement then javascript will automatically insert a semicolon.

An example of how this works. If javascript sees:

let a
let b

It will first try to parse it as:

let a let b

This is of course a syntax error. So javascript will try to insert a semicolon at the end of line and try again:

let a;
let b

This leads to it being parsed as:

let a; let b

Which is valid syntax so javascript is happy.

However, say you give javascript this code:

let a
[a, b] = [10 20]

Javascript will try to parse it as:

let a[a, b] = [10 20]

This is valid syntax since a[x] is just accessing the property x of a. But what about the comma? It is still valid syntax. It is called the comma operator. The comma operator is an operator just like + or -. The way the comma operator works is:

  1. evaluate the left side of the comma then ignore the result
  2. evaluate the right of the comma then return the result.

So the code:

a = 1, 2

Is the same as:

1
a = 2

This may seem completely useless but it is sometimes useful (only sometimes - most people never find a use for it). But, it is part of javascript syntax so the code:

a[a,b]

is valid syntax. It just means a[b].

So javascript has a different understanding of your code than you do.

Removing semicolon from javascript code was popularized by the standard.js project. If you want to use javascript without semicolons YOU MUST FOLLOW STANDARD.JS RULES. Otherwise do what I do and always use semicolons.

You can find rules for how not to use semicolons at: https://standardjs.com/rules.html#semicolons

It includes rules like:

Never start a line with (, [, `, or a handful of other unlikely possibilities.

Standard.js wants you to write your code like this:

let a = 1
let b = 2
;[a, b] = [b, a]   // <<------- semicolon at start of this line!!

Personally I prefer seeing semicolon at the end of every line but that's up to you.

slebetman
  • 109,858
  • 19
  • 140
  • 171
-3

You can use here the simple method.

let a = x;
let b = y;
let temp = a;
a = b;
b = temp;

I think it's best and fast method.

Sherzod
  • 303
  • 2
  • 8