Why are the results executed by JS ending with a semicolon inconsistent with those without a semicolon?

Code 1:
var a = "Aaa";
var b =" Bbb";
[a, b] = [b, a];
console.log (a);
console.log (b);

result:
Bbb
Aaa

< hr >

Code 2:
var a = "Aaa"
var b =" Bbb"
[a, b] = [b, a]
console.log (a);
console.log (b);

result:
Aaa
[undefined, "Aaa"]

if JS doesn"t force each line to end with a semicolon, shouldn"t the results of the above two pieces of code be the same?

Test environment: node v6.10.2

Mar.06,2021

Just because

does not force a semicolon does not mean that it is correct not to write a semicolon. The interpreter will automatically add a semicolon. If you don't guarantee that you can add a semicolon exactly as you want, you may add the wrong semicolon, and the result will be wrong. Become

var a = 'Aaa';
var b = 'Bbb'[a, b] = [b, a];
console.log(a);
console.log(b);

in fact, the code is the same as speaking, adding a semicolon is like adding punctuation. If you add a semicolon, the machine will go as you want. if you don't add a semicolon, the machine will follow its understanding.

give an example:

you say: I won't stay on rainy days.

that is understood by others as:
1. Stay on rainy days, but I will not stay on rainy days.
2. Stay on rainy days, but I won't stay.
3. A visitor on a rainy day, but a visitor on a rainy day? No.
4. If you are a guest on a rainy day, will you stay with me? Stay.
5. If it rains, it will be a guest. I won't stay if it rains.
6. Rainy days, guest days, stay with me? No.
7, rainy days, guest days, do you want me to stay? Stay.
8. Should I stay on rainy days or guest days?

The

code also has the same meaning, and the different results are just because of ambiguity. The above two answers are well explained, so I won't repeat them.


not everywhere without a semicolon. Your problem is:

var b = 'Bbb'[a, b] = [b, a];

it's actually quite interesting to look back at this question. So let me continue with the analysis:

=

< H1 > explain in detail < / H1 >

Why does b output at this time: [undefined, "Aaa"]?

extracurricular knowledge: first of all, there is a link

var a = b =c;

where breadc is an assignment statement and an expression, which is actually var a = (breadc) , which is equivalent to:

b=c;
a=c;

back to the original topic:
originally used [a code b] = [bjournal a] is an array exchange assignment with structural assignment. Once
but, becomes: var b = 'Bbb' [a, b] = [b, a] , the meaning changes to:

var b = [b, a]

this is simple, obviously assigning an array to b, but the first element of the array b is undefined!
so output:
b = [undefined, "Aaa"]


it is not mandatory, but without ambiguity. Your second sentence and your third sentence are ambiguous and can be merged into one sentence.

give me another example

   

just look at the picture. I installed Eslint in vscode, and those who can automatically format and correct errors will automatically format and correct errors, so you can see the execution logic of the parser.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3bedd-34682.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3bedd-34682.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?