Only before }, after one or more line breaks, and the end of program input are inserted
That is to say, you can only omit semicolons where a line, a block of code, and a piece of program ends.
That is to say, you can write the following code
The code copy is as follows:
function square(x) {
var n = +x
return n * n
}
But it cannot be written like the following code, so it will report an error
The code copy is as follows:
function area(r) { r = +r return Math.PI*r*r }//error
Insert only if subsequent input marks cannot be parsed
In other words, semicolon insertion is an error correction mechanism. Read the code and speak
The code copy is as follows:
a = b
(f())
//It can correctly parse into a separate statement with unit price of the following statement.
a = b(f())
a = b
f()
// parsed into two independent statements
a = bf();//The parsing is incorrect
So you have to pay attention to the beginning of the next statement to determine whether you can legally omit the semicolon.
(, [, +, -, and /, then it is best not to omit the semicolon in front of it.
Give an example to illustrate
The code copy is as follows:
a = b
['r', 'g', 'b'].forEach(function (key) {
console.log(key);
});
You originally thought there was no error, but the parser parsed into the following statement
The code copy is as follows:
a = b['r', 'g', 'b'].forEach(function (key) {
console.log(key);
});
Because the second sentence starts with [, the parser will not automatically insert a semicolon after the first statement, so it is parsed as shown above. Isn't b['b'].forEach wrong when parsing the above equation?
Therefore, if the statements starting with the five characters (, [, +, -, and / are best not to omit the semicolon before it.
If you want to omit the semicolon, an experienced programmer will follow the statement to ensure that the parser parses correctly. As shown below
The code copy is as follows:
a = b
var x//A declaration statement is specially added here to ensure that a = b will not be parsed with (f())
(f())
So if you need to omit a semicolon, you must check if the start mark of the next line is the above five characters, causing the parser to disable the automatic insertion of semicolons, or you can also prefix the five characters (, [, +, -, and /
Omitting semicolons causes script connection problems
The code copy is as follows:
//file1.js
(function () {
//...
})()
//file2.js
(function () {
//...
})()
When the above two files are connected, they will be parsed as follows
The code copy is as follows:
(function () {
//...
})()(function () {
//...
})()
Therefore, omitting semicolons not only requires you to be careful not only about the next tag of the current file, but also about any tag that may appear after the statement after the script is connected.
To avoid parser parsing errors, you can prefix an extra semicolon on each file to protect the script from careless connections. If the initial statement of the file is switched with the above 5 fragile character, you should add an additional semicolon prefix.
JavaScript syntax restriction generation
JavaScript syntax restriction production: No line breaks are allowed between two characters.
Give an example:
The code copy is as follows:
Return
{};
The above code is parsed into
The code copy is as follows:
return;
{}
;
The semicolon insertion rules for self-increase and self-decrease operations
The code copy is as follows:
a
++
b
Think about what the above code will be parsed into? Let's tell the answer, because the autoincrement operator can be used as both a preset operator and a postset operator, but the postset operator cannot appear before the line break, so the above code is parsed into
The code copy is as follows:
a;
++b;
The semicolon will not be automatically inserted as a separator at the head of the for loop empty statement.
The code copy is as follows:
for (var i = 0,total=1
i < length
i++) {
total*=i;
}
Codes like the above will have parsing errors.
The while of the empty loop body also needs to be displayed semicolons, otherwise it will also lead to parsing errors.
The code copy is as follows:
function mytest() {
While (true)
}
Must be written as follows to avoid errors
The code copy is as follows:
function mytest() {
while (true) ;
}
Let's summarize
1. Deduce semicolons only before the } tag, at the end of a line, and at the end of the program
2. Deduce semicolons only when the following mark cannot be parsed
3. You must not omit semicolons before statements starting with (, [, +, -, and / characters.
4. When the script is connected, explicitly insert semicolons between the scripts
5. You must not wrap the line before the parameters of return, throw, break, continue, ++ or --
6. A semicolon cannot be deduced as a separator for the header of a for loop or an empty statement.