JS Tutorial: Talking about ECMAScript insert semicolon automatically

Source: Internet
Author: User
Tags continue empty end expression insert return

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

The rules for inserting semicolons automatically in ECMAScript are also explained: 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, in specific cases, these semicolons in the source code text can be omitted. in other words, the end of these statements does not require a hard input semicolon, and JavaScript automatically inserts the end of the statement.

To learn more about the ECMAScript Semicolon Auto Insert rule you can view the following links:

    • Original
    • Chinese translation

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//No semicolon is inserted automatically
A<b//No semicolon will be inserted automatically
a++
)
{}

ECMAScript also explains that it is a special case to interpret a semicolon as an empty statement and not to insert a semicolon in the () in a For statement, which is not part of the automatic insertion rule.

Although JavaScript is a weakly typed language, ECMAScript's semicolon automatic insertion rule is hard to understand. But develop good code writing habits, manually inserted semicolon, develop habits, you can avoid these problems, but also in the program debugging, code reading on their own to others are not small help.

At the same time, ECMAScript gave the programmer some advice:

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


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.