The semicolon of javascript represents the ending character of a statement, but since javascript has the automatic semicolon insertion rule, it is a very easy thing to make people confused. In general, a newline will produce a semicolon, but the actual situation is Otherwise, in JavaScript, a new line may or may not be generated. Whether a semicolon is automatically inserted depends mainly on the up and down line. So even experienced programmers sometimes have big heads.
There are also corresponding explanations for the automatic insertion rules of semicolons in ECMAScript: null statements, variable statements, expression statements, do-while statements, continue statements, break statements, return statements, and throw statements. These determined ECMAScript statements must be divided into The end of the number. These semicolons can always appear explicitly in the source code text. For convenience, in certain cases, these semicolons in the source code text may be omitted. In other words, the end of these statements does not require a hard input semicolon ending, and JavaScript will automatically insert the end of the statement.
If you want to know the detailed ECMAScript semicolon automatic insertion rules, you can view the following link:
original
Chinese translation
Practice the truth, look at the following examples and you will understand that automatic insertion of semicolons is not so easy to understand. A little bit of attention will make you feel overwhelmed.
The bloody incident caused by return
The following is the quoted content: function test(){ |
A function that returns a+b value has no problem at first, but the result of running alert is undefined. According to the automatic insertion rule of semicolon, if there is a new line 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 a newline, you can do this:
The following is the quoted content: function test(){ (function (){ |
Very weird, can't explain, can anyone tell me ~
Two semicolons in the header of the for statement do not automatically insert semicolons
The following is the quoted content: for( var a=1,b=10 //The semicolon will not be inserted automatically |
ECMAScript also explains the above: interpreting semicolons as empty statements and not automatically inserting semicolons in () in the for statement is a special case and is not subject to the automatic insertion rule.
Although javascript is a weak-type language, ECMAScript's semicolon automatic insertion rules are difficult to understand thoroughly. However, developing good code writing habits, manually inserting semicolons, and developing habits can avoid these problems. At the same time, it will be of great help to yourself and others in program debugging and reading code.
At the same time, ECMAScript also gives programmers some advice:
++ or — should appear on the same line as its operand.
The expression in the return or throw statement should appear on the same line as return or throw.
The tag in the break or continue statement should appear on the same line as break or continue.