Rules for automatically inserting semicolons in JS

Source: Internet
Author: User

Today, I modified the previous JS syntax analysis and reviewed the rules for automatically inserting semicolons. Probably as follows (ecma262 V5, 7.9 ):

1. WhenCodeDuring parsing, an invalid token is found (this token is called an offending token at this time). In the following cases, a semicolon is automatically inserted:

    1. There is a line break before the token
    2. This token is}

2. An unexpected end will be inserted with a semicolon.

3. A valid token appears, but this location is in a special statement environment (in the ecma262 syntax description, this location is called [No lineterminator here]), and there is a line break before this token, in this case, a semicolon is inserted after the previous token.

Postfixexpression:
Lefthandsideexpression [No lineterminator here] ++
Lefthandsideexpression [No lineterminator here] --
Continuestatement:
Continue [No lineterminator here];
Breakstatement:
Break [No lineterminator here];
Returnstatement:
Return [No lineterminator here] expression;
Throwstatement:
Throw [No lineterminator here] expression;

HoweverIf the inserted Semicolon is an empty statement or a part of the for header, the semicolon is not inserted.

Finally, although the JS engine will automatically insert a semicolon, it is best not to use this feature. Instead, write the semicolon in full, so there are not many.

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.