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.
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.
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:
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.
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.