"JavaScript Zero-based" language core section----optional semicolon

Source: Internet
Author: User

Learning Notes is primarily about documenting some of the things JavaScript needs to pay attention to, and some small details.

Optional semi-colon

Like many other programming languages, JavaScript uses semicolons (;) separate statements. This is important for enhancing the readability and cleanliness of your code.

Without a delimiter, the end of a statement is the beginning of the next-hop statement, and vice versa.

In JavaScript, if the statement is a single line, you can usually omit the semicolon (or the semicolon before the end of the program or the curly Brace "}") between the statements.

Many JAVASRITPT programmers use semicolons to clarify the interpretation of statements, even when semicolons are not fully needed.

I also suggest that you do so.

The following code,

A=3; b=4; // because two statements are written in two lines, the first semicolon can be omitted.
A=3;b=4; // If you are on one line, the first semicolon cannot be omitted

It is important to note thatJavaScript does not have to fill the semicolon with all line breaks, and JavaScript does not fill the semicolon until the code is parsed correctly without a semicolon.

var aa=3console.log (a)//var A;  A=3;  Console.log (a)

Look at the following example,

var y=x+F (a+b). toString ()// The parentheses of the second line and the F of the first line make up a function call //  JavaScript is parsed into var y=x+f (a+b). toString ();

And the code above is not meant to be like this. The semicolon is better not to omit it.

In general, if a statement starts with a "(", "[", "/", "+", or "-" , it is most likely resolved with the previous statement.

Statements that start with "/", "+" or "-" are uncommon, and statements that start with "(", "[" are very common, at least in some javescript coding styles.

Some programmers prefer to keep a semicolon in front of the statement so that even if the previous statement is modified and the semicolon is mistakenly deleted, the current statement will parse correctly:

var // the semicolon is omitted here ; [X,x+1,x+2].foreach (Console.log)// front semicolon guarantees correct statement parsing

If the current statement and the next line of statements cannot be parsed, JavaScript fills the semicolon after the first line, which is a common rule, with two exceptions.

The first exception is in a scenario involving return, break, and continue statements. If the three keywords are followed by a newline, JavaScript fills the semicolon at the line break.

For example:

return ture; // JavaScript will parse into: return ;  true ; // and the code is meant to be like this: return   true;

The second exception is when the "+ +" and "---" operators are involved.

These operators can be either prefixed to an expression or as a suffix of an expression.

If you use it as a suffix expression, he and the expression should be on the same line.

Otherwise, the end of the line will fill the semicolon, and the colleague "" + + or "--" will be used as the prefix operator for the next line of code and resolved previously.

For example:

x+ +y//

Finally, we remind you to use semicolons to clarify the interpretation of statements, even when semicolons are not completely needed.

"JavaScript Zero-based" language core section----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.