How to prevent JavaScript from automatically inserting semicolons _javascript tips

Source: Internet
Author: User

In JavaScript, the semicolon at the end of the line has an automatic insertion mechanism that allows some friends to ignore the input semicolon. Of course you'd better get into the habit of typing a semicolon and mastering how JavaScript handles ignoring the input semicolon, because it helps you understand code without semicolons.

The JavaScript language has a mechanism: when parsing, you can automatically insert a semicolon after a sentence to modify the semicolon delimiter that is omitted from the end of the statement. However, because this pluggable semicolon is in conflict with another mechanism of the JavaScript language, all spaces are ignored, so the program can format the code with a space.

The conflict between the two mechanisms is apt to obscure more serious parsing errors. Sometimes it is inappropriate to insert a semicolon. For example, inserting a semicolon automatically in a return statement can result in the consequence that if the return statement returns a value, the beginning part of the expression of the value must be on the same line as returned, for example:

 var f = function () {
 return
 {
 status:true
 };
 }

Looks like it's going to return an object that contains the status member element. Unfortunately, JavaScript automatically inserts a semicolon so that it returns undefined, which causes the objects that are actually returned below to be ignored.

When an auto insert semicolon causes the program to be misunderstood, there is no warning alert. You can avoid this problem if you put {on the end of the previous line rather than the head of the next line, for example:

 var f = function () {return
 {
 status:true
 };
 }

To avoid omitting the semicolon-caused error, it is recommended that you develop a good habit, whether the statement is complete or not, as long as the complete statement must add a semicolon to indicate the end of the sentence.

To facilitate reading, when a long sentence needs to be displayed in a branch, make sure that the line does not form a complete logical semantics within a row. For example, the following code is a continuously assigned statement that can be viewed more clearly by a branch display. This branch shows that JavaScript does not see each row as a separate sentence, and thus does not create ambiguity, because it cannot form a single logical semantics within a row.

 var a =
 b =
 c = 4;

The above statement appears as follows in one line: var a = b = c = 4;

For the following statement, it is easy to create ambiguity if the branch is not displayed correctly. The meaning of the sentence: Define a variable i, it is then assigned a value, if variable A is true, the assignment is 1, otherwise the variable B is judged, and if B is true, the assignment is 2, otherwise the variable C is judged, and if C is true, the assignment is 3, or the assignment is 4.

 var i = a? 1:b? 2:c? 3:4;

The following branch display is wrong because of the expression a? 1:b can form independent logical semantics, so JavaScript automatically adds a semicolon later to represent a separate sentence.

 var i = a? 1:b
 2:c
 3:4;

A secure method should be shown in the following branches so that each row cannot form a separate semantics.

 var i = a? 1
 : B. 2
 : C. 3
 : 4;

In short, when you write code, you should develop a good habit of using semicolons to end sentences, which should be separated with semicolons. The lines displayed in the sentence should ensure that a single line does not easily form independent, legitimate logical semantics.

PS: Sample details JavaScript automatically adds a semicolon at the end of a line

semicolons (;), usually at the end of a line of statements, the code is as follows:

var webName = "cloud-dwelling community";
var url = www.jb51.net;

The above code, after each declaration statement, adds a semicolon, which does not have to be explained more.

var webName = "Cloud Habitat Community"
var url = www.jb51.net

The semicolon can also be omitted, but the omitted semicolon will be automatically added to the semicolon at compile time.
Let's take a look at some code examples:

function done (webName) {
 return
 webName
}
console.log ("cloud-dwelling community");

Because the compiler automatically adds a semicolon after each line, the output value is undefined, not the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.