The semicolon in JavaScript represents the end of the statement, but because JavaScript has automatic semicolon insertion rules, it is a very easy thing to blur. Under normal circumstances, a newline will generate a semicolon, but the actual situation is not Otherwise, that is to say, a semicolon may or may not be generated when a line breaks in JavaScript. Whether a semicolon is automatically inserted depends mainly on the upper and lower lines. So even experienced programmers sometimes have headaches.
There are also corresponding explanations for the automatic semicolon insertion rules in ECMAScript : empty statements, variable statements, expression statements, do-while statements, continue statements, break statements, return statements, and throw statements. These certain ECMAScript statements must be separated by The number ends. These semicolons can always appear explicitly in the source code text. For convenience, these semicolons in source code text may be omitted in certain cases. In other words, there is no need to enter a hard semicolon at the end of these statements. JavaScript will automatically insert it at the end of the statement.
If you want to know the detailed ECMAScript semicolon automatic insertion rules, you can view the following link:
Practice brings out the truth. Take a look at the following examples and you will understand that the automatic insertion of semicolons is not so elusive. A little carelessness can give you a headache.
Murder caused by return
The following is the quoted content: function test(){ |
A function that returns the value of a+b looks fine at first glance, but the result of running alert is undefined. According to the automatic insertion rules of semicolons, if there is a line break after the return statement, the semicolon will be automatically inserted, and it is easier to understand if there is no return value. If you need to wrap the line, you can do this:
The following is the quoted content: function test(){ (function (){ |
The two semicolons in the for statement header are not automatically inserted.
The following is the quoted content: for( var a=1,b=10 // Semicolon will not be automatically inserted |
ECMAScript also has an explanation for the above: interpreting semicolons as empty statements and not automatically inserting semicolons in () in for statements are special cases and are not governed by automatic insertion rules.
Although JavaScript is a weakly typed language, ECMAScript's automatic semicolon insertion rules are difficult to understand thoroughly. However, by developing good code writing habits, manually inserting semicolons and forming a habit, you can avoid these problems. At the same time, it will be of great help to yourself and others in program debugging and code reading.
At the same time, ECMAScript also gives programmers some advice:
++ or — should appear on the same line as its operands.
The expression in a return or throw statement should appear on the same line as return or throw.
Labels in a break or continue statement should appear on the same line as the break or continue.