-3

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

Kevin
  • 209
  • 2
  • 11

1 Answers1

2

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • but i dont think the first **semicolon** you wrote after the **var x** statement is an empty statement i think its also a *semicolon* – Kevin Apr 25 '20 at 15:19
  • 1
    Yes, every semicolon is a semicolon. They **separate** statements. – Pointy Apr 25 '20 at 15:21
  • then what is an **empty statement** [ECMAScript](https://tc39.es/ecma262/#prod-EmptyStatement) and [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/Empty) talks about – Kevin Apr 25 '20 at 15:26
  • 1
    The empty statement is what's between the two semicolons. It's kind-of a zen thing. – Pointy Apr 25 '20 at 15:29
  • what's between the two semicolons though there in only a whitepace character ? `var x; ;` – Kevin Apr 25 '20 at 15:30
  • 1
    Exactly. Why do you think the "empty statement" is called the "empty statement"? – Pointy Apr 25 '20 at 15:31
  • but [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/Empty) and the [ECMAScript](https://tc39.es/ecma262/#prod-EmptyStatement) specification haven't stated that a `whitespace` is what known as the `empty statement` but they have stated that the `semicolon` is the empty statement – Kevin Apr 25 '20 at 15:35
  • 1
    @Kevin no. The empty statement is the lack of anything other than whitespace between two semicolons. Thus, with `var x = 1;;` there's also an empty statement. You're misreading the article on MDN; really the spec itself is the authoritative source of information. On top of that, learning the language by examining the minutiae of its grammar is a really bad idea. Nobody programming in JavaScript on a daily basis worries about what an "empty statement" is. – Pointy Apr 25 '20 at 18:24
  • okay i found that the "semicolon" is not a statement all by itself – Kevin Apr 27 '20 at 16:10