The JavaScript language has a mechanism: during parsing, a semicolon can be automatically inserted after a sentence to modify the semicolon separator missing at the end of the statement. However, because this automatically inserted semicolon conflicts with another mechanism in the JavaScript language, that is, all space characters are ignored, the program can use spaces to format the code in JavaScript, the semicolon at the end of a row has an automatic insertion mechanism, so that some friends can ignore the input semicolon. Of course, you 'd better get into the habit of entering a semicolon and know how JavaScript handles ignoring the input semicolon, because this knowledge helps you understand the code without a semicolon.
The JavaScript language has a mechanism: during parsing, a semicolon can be automatically inserted after a sentence to modify the semicolon separator missing at the end of the statement. However, because this automatically inserted semicolon conflicts with another JavaScript mechanism, that is, all space characters are ignored, the program can use spaces to format the code.
Conflicts between the two mechanisms can easily conceal more serious parsing errors. Sometimes a semicolon is inserted out of date. For example, if a semicolon is automatically inserted in a return statement, the result is as follows: if a return statement returns a value, the start part of the expression must be in the same line as return. For example:
var f = function(){ return { status: true }; }
It seems that an object containing the status member element is returned here. Unfortunately, JavaScript automatically inserts a semicolon so that it returns undefined, which causes the objects to be returned to be ignored.
When a program is misunderstood because a semicolon is inserted automatically, no warning is triggered. If you put {at the end of the previous line rather than the header of the next line, you can avoid this problem, for example:
var f = function(){ return { status: true }; }
To avoid errors caused by semicolons, we recommend that you develop a good habit. no matter whether the statements in a row are complete or not, you must add a semicolon to indicate the end of the sentence as long as the statements are complete.
To facilitate reading, when long sentences need to be displayed by branch, make sure that the complete logical semantics cannot be formed within a line. For example, the following code is a continuous value assignment statement. You can view their relationships more clearly through the branch display. This branch shows that JavaScript does not regard each row as an independent sentence because it cannot form an independent logical semantic within a row, and thus does not produce ambiguity.
var a = b = c = 4;
The preceding statement is shown in a row as follows: var a = B = c = 4;
If the following statement cannot be correctly displayed by branch, ambiguity may occur. Definition of the sentence: defines a variable I and assigns a value to it. If variable a is true, the value is 1. Otherwise, variable B is judged. If B is true, the value is 2; otherwise, the variable c is judged. If c is true, the value is 3; otherwise, the value is 4.
var i = a ? 1 : b ? 2 : c ? 3 : 4;
The branch shown below is incorrect because expression? 1: B can form independent logical semantics, so JavaScript will automatically add a semicolon after it to represent an independent sentence.
var i = a ? 1: b ? 2 : c ? 3 : 4;
The security method should adopt the following branch display, so that each row cannot form an independent semantics.
var i = a ? 1 : b ? 2 : c ? 3 : 4;
In short, when writing code, you should develop a good habit of ending sentences with semicolons. All complete sentences should be separated with semicolons. The sentence displayed by the Branch should ensure that the single line is not easy to form independent legal logical semantics
PS: for example, javascript automatically adds a semicolon at the end of a row
A semicolon (;) is usually used at the end of a line of statements. The Code is as follows:
Var webName = ""; var url = www.jb51.net;
The above Code adds a semicolon after each declaration statement, which does not need to be explained.
Var webName = "" var url = www.jb51.net
In fact, the semicolon can be omitted, but the semicolon will be automatically added to it during compilation.
Let's take a look at a code example:
Function done (webName) {return webName} console. log (done (" "));
Since the compiler automatically adds a semicolon to the end of each line, the output value is undefined, which is not "script home ".