3

How come in JavaScript you can call a function without ;?

Example

Let’s say testMethod is declared somewhere...

testMethod();

testMethod() // Without semicolon will still work.

Also calling a method within an element example:

<input type="button" onclick="testMethod();" />
<input type="button" onclick="testMethod()" /> // Again no semicolon.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1349313
  • 143
  • 2
  • 7
  • semi colons are required to separate statements. Since there's only a single statement in your onclick, there's an implied/invisible ; in there between the `)` and the `"`. – Marc B May 14 '12 at 18:41
  • what about inside the – user1349313 May 14 '12 at 18:42
  • same thing. if the script block ends, there's an implied ; just before the end of the block. – Marc B May 14 '12 at 18:43
  • 1
    same deal, one statement per line, javascript can handle this distinction and will insert the `;` for you. If you had multiple statements per line i believe you would need to insert the `;` after each statement on the same line. – Mike McMahon May 14 '12 at 18:43
  • so if I have 2 statements It will be an error inside – user1349313 May 14 '12 at 18:43
  • 3
    Read the comments and answers - you can have 100 lines of code with no semicolons with no errors as a result. Personally, I insert the semicolons for clarity. – Surreal Dreams May 14 '12 at 18:44
  • SO IN SHORT JUST USE ; END OF DISCUSSION ^_^. – user1349313 May 14 '12 at 18:49
  • @user1349313: Not at all. Be knowledgeable about where/how the ECMAScript specification requires automatic insertion, and write your code to follow the spec. – cliffs of insanity May 14 '12 at 18:56

3 Answers3

32

It's a feature called "Automatic Semicolon Insertion" (or ASI).

From Dealing with JavaScript's Automatic Semicolon Insertion

So how are these semicolons inserted for you? By following these rules (paraphrased and simplified from ECMA-262 3rd edition, 7.9.1):

  • When, as the program is parsed, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true:
    1. The offending token is separated from the previous token by at least one LineTerminator.
    2. The offending token is }.
  • When the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete program, then a semicolon is automatically inserted at the end of the input stream.
  • A token is a restricted token when it is allowed by some production of the grammar, but the production is a restricted production and the token would be the first token for a terminal or nonterminal immediately following the annotation “[no LineTerminator here]” within the production.

If furthermore the restricted token is separated from the previous token by at least one LineTerminator, then a semicolon is automatically inserted before the restricted token.

...

Mike McMahon
  • 7,096
  • 3
  • 30
  • 42
jmar777
  • 38,796
  • 11
  • 66
  • 64
  • 1
    [An Open Letter to JavaScript Leaders Regarding Semicolons](http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding) – cliffs of insanity May 14 '12 at 19:05
2

Because the JavaScript engine places semicolons at the end of line if it thinks they should have been there. This often introduces bugs rather than avoiding them:

return
{
  user: 'foo'
}

The return statement above returns... nothing:

return;
{
  user: 'foo'
}
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 2
    "Rarely" would be more accurate than "often" — if it were often, nobody would think ASI worked reliably. – Chuck May 14 '12 at 18:43
  • 1
    Just my $.02, but I agree. I just found a 2 lines of code were I left off the semicolon on accident, and was curious as to *why* it was still working without any issue. – Xander Luciano May 15 '17 at 14:57
2

JavaScript interpreters will add semicolons for you. Omitting semicolons is a very bad idea as it can cause interpreted JavaScript code to behave differently than you'd expect due to this.

See the breaking examples on Wikipedia:

return
a + b;

// Returns undefined. Treated as:
//   return;
//   a + b;

Related:

Do you recommend using semicolons after every statement in JavaScript?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jbabey
  • 45,965
  • 12
  • 71
  • 94