JavaScript semicolon Summary and detailed introduction to _javascript tips

Source: Internet
Author: User

JavaScript Each statement ends with a semicolon, but because JavaScript has a semicolon automatic insertion rule, all the different programmers have different habits, some with a semicolon, and some without a semicolon, then whether to add a semicolon or no semicolon good? This article explores JavaScript each statement should not add a semicolon.

The semicolon of JavaScript represents the Terminator of the statement, but because JavaScript has a semicolon that automatically inserts rules, it is a very confusing thing, and in general, a newline produces a semicolon, but the reality is not, That is, wrapping in JavaScript can result in a semicolon, or it may not occur, or whether to automatically insert a semicolon, mainly in the following line. So even experienced programmers sometimes get big heads.

JavaScript automatic plus semicolon rule:

    1. A semicolon is automatically filled when there are line breaks (including multiline comments with newline characters), and the next token cannot match the previous syntax.
    2. When there is a missing semicolon, the semicolon is filled.
    3. When the program source code ends, if the semicolon is missing, the semicolon is filled.

Next, we'll look at the syntax ambiguity caused by the no semicolon:

The first is + + and--the two operators appear at the end of the previous line, and the following line begins with a syntax ambiguity:
function delete void typeof new Null true false Numericliteral stringliteral regularexpressionliteral ([{Identifier + + -- + - ~ !
where function and delete are very commonly used statement at the beginning.
In particular, + + and--when individually broken into a row, because JS syntax rules after the self-add operation does not allow the middle insert line, so + + and--will be considered as the previous increase and the next line to join together.

The second is when return is the end of the previous line, and the next line begins with the following syntax ambiguity:
function delete void typeof ([{Identifier + +--+-~!)

Also because the rules of the JS syntax do not allow the insertion of line breaks between return and subsequent values, the return of a newline character as long as there is a semicolon will often be inconsistent with the user's expectations.

The third is where the next line starts with + and-the previous line ends with the following, resulting in grammatical ambiguity:
+ + + IdentifierName])} regularexpressionliteral

Because there are very few statements that start with a + or--this is not a dangerous situation.

The fourth is the last line to break, continue the end of the situation, the next line begins with identifier, will produce grammatical ambiguity.

The fifth is the next line (and the beginning of the case, the previous line at the end of the following is, will produce grammatical ambiguity:
--+ + IdentifierName])} regularexpressionliteral stringliteral numericliteral booleanliteral NullLiteral Identifier S
This is a very dangerous situation (so it should be written in Hax's article to write a semicolon), and almost all of the cases on the previous line will result in an exception to the normal expectation.

The sixth is that the next line begins with Regularexpressionliteral, and the following end of the previous line leads to/is understood as division:
-+ + IdentifierName])} regularexpressionliteral stringliteral numericliteral booleanliteral NullLiteral Identifier th Is

To practice the truth, take a look at these examples and see that it is not so subtle to insert semicolons automatically. A little attention will make your head big.

The bloodshed caused by return:

function test () {
  var a = 1;
  var b = 2;
  return//will automatically insert semicolon
    (
     a+b
    )
};
Alert (test ());

A function that returns the A+B value, but the result of running alert is undefined. According to the automatic insertion rule for semicolons, a semicolon is automatically inserted after the return statement if there is a line break, and it is better understood that there is no returned value. If you need to change lines, you can do this:

function test () {
  var a = 1;
  var b = 2;
  Return ( 
      a+b
     )
};
Alert (test ());

Two murders caused by closures:

(function () {
 var A;
}) ()//does not automatically insert a semicolon
(function () {
 var B;
}) ()

Very strange, can't explain, who can tell me ~

The two semicolon in the header of the For statement and does not automatically insert a semicolon

for (Var a=1,b=10//Does not automatically insert semicolons
     a<b//does not automatically insert semicolons
     a++ 
  )
{}

Summarize

    1. In return, break, continue, after self-add, after the five kinds of statements, line breaks can completely replace the role of the semicolon.
    2. The previous line plus a semicolon does not affect a single statement that starts with a few keywords, as well as a null statement, if the Var if do-for-continue break return-with switch throw try debugger a few words.
    3. Where expression statements and function expression statements are not followed by semicolons are dangerous and extremely complex.
    4. All (and [the beginning of statements, preceded by no semicolon is extremely dangerous.)

The above is the JavaScript semicolon data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.