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.
8
  • 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, 2012 at 18:41
  • what about inside the <script> May 14, 2012 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, 2012 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. May 14, 2012 at 18:43
  • 4
    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. May 14, 2012 at 18:44

3 Answers 3

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.

...

1
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'
}
2
  • 2
    "Rarely" would be more accurate than "often" — if it were often, nobody would think ASI worked reliably.
    – Chuck
    May 14, 2012 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. May 15, 2017 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?

2
  • 4
    The issue in your example has nothing to do with omitting semicolons. May 14, 2012 at 18:45
  • Bacause the COMPLETE RETURN command should be written in the same line when you go by no-semicolon. The correct is return a+b Aug 22, 2020 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.