"Go" javascript semicolon problem

Source: Internet
Author: User

The semicolon of the JavaScript represents the terminator of the statement, but since JavaScript has a semicolon-inserted rule, it is a very confusing thing, and in general, a line break produces a semicolon, but the reality is not , which means that wrapping in JavaScript can produce a semicolon or not, or whether to automatically insert a semicolon, which is primarily a downward line. So even experienced programmers sometimes get big heads.

There are also explanations for the automatic insertion of semicolons in ECMAScript: empty statements, variable statements, expression statements, do-while statements, continue statements, break statements, return statements, and throw statements. These determined ECMAScript statements must end with a semicolon. These semicolons can always appear explicitly in the source code text. For convenience, these semicolons in the source code text can be omitted under certain circumstances. This means that the end of these statements does not require a hard input semicolon ending, and JavaScript is automatically inserted at the end of the statement.

To learn more about the ECMAScript Semicolon auto-insert rule, you can view the following links:

    • Original
    • Chinese translation

Practice the truth, take a look at these examples, and understand that semi-automatic insertion is not so subtle. A little attention will make your head big.

A murder caused by return

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

A function that returns the A+B value is not a problem at first, but the result of running alert is undefined. According to the automatic insertion rule of the semicolon, if there is a line break after the return statement, the semicolon is automatically inserted and the return value is better understood. If you need to change lines, you can:

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 semicolons
(function () {
var b;
})()

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

Two semicolons in the For statement header, not automatically inserted semicolon

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

ECMAScript also explains that the semicolon is interpreted as an empty statement and that the semicolon is not automatically inserted in the For statement () is a special case and is not governed by an auto-insert rule.

Although JavaScript is a weakly typed language, ECMAScript's semi-colon auto-insertion rules are hard to understand. But develop good code writing habits, manual insertion of semicolons, habits, you can avoid these problems, at the same time, the program debugging, code reading on their own for others have no small help.

ECMAScript also gives the programmer some advice:

      • + + or-should appear on the same line as its operand.
      • The expression in the return or throw statement should appear on the same line as return or throw.
      • A label in a break or continue statement should appear on the same line as break or continue.
Related Article

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.