Semicolon for Javascript

Source: Internet
Author: User

The semicolon in Javascript indicates the end character of the statement. However, because JavaScript has a semicolon automatic insertion rule, some people think that it is not necessary to write the statement. In general, A line feed produces a semicolon, but this may not be the case. That is to say, a line feed in Javascript may generate a semicolon or not. whether to insert a semicolon automatically depends on the downstream.

In ecmascript, the automatic insert rules for semicolons are also explained as follows: NULL statements, variable statements, expression statements, do-while statements, continue statements, break statements, return statements, and throw statements, these identified ecmascript statements must end with a semicolon. These Semicolons can always appear explicitly inSource codeText. For convenience, the sourceCodeThe semicolons in the text can be omitted. That is to say, the end of these statements does not need to end with a rigid Semicolon. Javascript will automatically Insert the end of the statement.

See the following

Case of blood caused by return

Function Test(){VaRA =1;VaRB =2;Return // The semicolon is automatically inserted.(A + B)}; alert (test ());

A function that returns the + B value has no problem at first glance, but the result of running alert is undefined. According to the automatic insert rules of semicolons, if the return statement is followed by a line break, the semi-colon will be inserted automatically. It is easier to understand if no return value is returned. If you need a line feed, you can:

Function Test(){VaRA =1;VaRB =2;Return(A + B)}; alert (test ());

Couple of two closures

 
(Function (){VaRA ;})()// The semicolon is not automatically inserted.(Function (){VaRB ;})()

It's weird. I can't explain it. Can someone tell me ~

Two semicolons in the for statement header are not automatically inserted.

 
For(VaRA =1, B =10 // The semicolon is not automatically inserted.A <B// The semicolon is not automatically inserted.A ++ ){}

Ecmascript also has an explanation of the above: interpreting a semicolon as an empty statement and not automatically inserting a semicolon in () in a for statement is a special case and does not fall under the jurisdiction of an automatic insertion rule.

Although Javascript is a weak language, it is hard to understand the automatic insertion rules of semicolons in ecmascript. However, to avoid these problems, you can develop good code writing habits, manually insert semicolons, and develop habits.ProgramDebugging, packaging, compression, and code reading are of great help to others.

At the same time, ecmascript also gives programmers some advice:

++ Or-must be in the same row as its operand.
The expression in the return or throw statement should be in the same line as the return or throw statement.
The label in the break or continue statement should be in the same line as the 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.