JS Learning the fourth day----optional semicolon

Source: Internet
Author: User

Optional semi-colon

As in some languages,, JSusing semicolons(;)separate the statements.This is important to enhance the readability and cleanliness of your code.:missing delimiter,The end of a statement is the next statement,you seem to be talking to someone on the walkie-talkie.,I didn't finish a word.,have to say Oversame,It means you're done..in theJSin,if the statements are exclusive to each row,patency can omit a semicolon between statements.manyJSthe programmer uses semicolons to explicitly mark the end of a statement,It is also true when a semicolon is fully needed in propane.another style is,omit it in any place where it can be omitted.,use semicolons only when you have to use them..whatever it takes to become a style,about theJSThere are a few things to be aware of in the optional semicolon.

Case :

A=3;

b=4;

Analysis : Since two statements are written in two lines , the first semicolon , that is to say , the semicolon of the a=3 sentence can be omitted .

But if you write this :

a=3;b=4;

Analysis : This will not omit the first semicolon.

It is important to note that JS does not fill the semicolon at all line breaks : js will fill the semicolon only if the code is not parsed correctly when the semicolon is missing. . In other words, , if the current statement and subsequent non-whitespace characters cannot be interpreted as a whole , JS fills the semicolon at the end of the current statement line . Case :

var a

A

=

3

Console.log (a)

JS interprets it as :

var a;a=3;console.log (a);

JS adds a semicolon to the first line of line breaks , because if there is no semicolon , JS cannot parse the code var A. a second a can be treated as a single statement "a"; but , JS and did not give the second line the end of the fill semicolon , because he can parse it into "A=3" with the third line of content.

I do not know that handsome man (SB) invented this situation , We are still the old honest line statement a semicolon . try not to engage in heinous acts. . I say these questions so that you can read the code that someone else wrote. , after all, a wonderful day , very much today . .

The segmentation rules for these statements lead to unexpected situations , which are written in two lines and appear to be two separate statements :

var y=x+f

(A+B). ToString ()

But the second line of parentheses and the first line of F make up a function call , JS will consider this code :

var y=x+f (a+b). toString ();

This is not the case for the northern part of this code . in order to allow the above code to parse into two different statements , You must manually fill in the display semicolon at the end of the line .

Generally speaking,If a statement"(", "[", "/", "+" or "-" start,then it is very likely to parse with the previous set of statements.in"/", "+" and "-" are uncommon for statements that start with,the person who writes this code is far better off..but with"(" and "[" Start with a lot of statements,At least inJSis very common in the.Some more cautious programmers prefer to add a semicolon to the front of the statement,This is done to avoid forgetting the semicolon of the previous statement.Case:

var x=0// Forget the semicolon here

; [X,x+1,x+2].foreach (Console.log);// The semicolon in front ensures correct parsing of the statement

If the current statement and the next line of statements cannot be parsed , JS fills the semicolon after the first line , which is a general rule , with two exceptions . The first exception is in relation to Return,break and the Continue statement in the scene . If these three keywords are followed by a newline , JS The semicolon is filled in a newline. . Case :

Return

True

Analysis : JS will parse into : return; true; , but JS 's intention is to return true;

that is, There can be no line break between Return,break, and continue and subsequent expressions . If a line break is added , The program will only get an error if it is in very special circumstances. , and the program is very inconvenient to debug .

The second exception really involves the time of the "+ +" and "--" operators . These operators can be prefixed with an expression , can also be used as the suffix of an expression . If you use it as a suffix expression , He and the expression should be on the same line . otherwise , end of line will fill the semicolon , at the same time "+ +" or "-" will be used as the prefix operator for the next generation of code and parse it together , case :

X

++

Y

This code will be parsed as " x;++y" instead of " x++;y".

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JS Learning the fourth day----optional semicolon

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.