简体   繁体   中英

Can someone explain me how the “empty statement” in javaScript is affected from “Automatic Semicolon Insertion”

Can someone explain me how the "empty statement" in javaScript is affected from "Automatic Semicolon Insertion" The MDN website states that the empty statement is affected from automatic semicolon insertion though its not stated in the ECMAScript specification can anyone explain this to me and can anyone explain me what is the difference between a semicolon and an empty statement

Read the spec linked from the MDN page. The empty statement is "affected" because ASI won't be done if the inserted semicolon would result in an empty statement.

Hand-wringing over when ASI happens can be dispensed with by simply including semicolons explicitly. The most common pitfall of ASI is the return statement, when attempting something like

return
  { propertyName: "something" };

Don't do that. Start the object initializer on the same line as the return .

As to the difference between a semicolon and an empty statement: a semicolon is a boundary. In the following code:

var x; ;

There's a var declaration statement, then an empty statement. The second semicolon is not part of the empty statement, but it implies that there is an empty statement before it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM