The necessity of a semicolon for JavaScript statements

Source: Internet
Author: User

statements in ECMAScript is terminated by a semicolon, though omitting the semicolon makes
The parser determine where the end of a statement occurs, as in the following examples:
var sum = a + b       //valid even without a semicolon-not recommendedvar diff = b;     Valid-preferred


even though a semicolon is no required at the end of statements, it's recommended to always
Include one.
Including semicolons helps prevent errors of omission, such as not finishing what are you
Were typing, and allows developers to compress ECMAScript code by removing extra white space
(Such compression causes syntax errors when lines does not end in a semicolon). Including semicolons
Also improves performance in certain situations, because parsers try to correct syntax errors by
Inserting semicolons where they appear to belong.


For example, the following code:

"Use strict" function f (A, B) {    return    a | b;} Console.log (f (2, 1));

Output:


So, let's not let the parser guess our intentions, after all, the parser is not artificial intelligence:

Correct wording:




The necessity of a semicolon for JavaScript statements

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.