Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:14:27+00:00 2026-05-22T21:14:27+00:00

Possible Duplicate: What are the rules for Javascript's automatic semicolon insertion? JavaScript befuddles me

  • 0

Possible Duplicate:
What are the rules for Javascript's automatic semicolon insertion?

JavaScript befuddles me with its implicit line termination. It’s a very C-like language, except that ending lines in a semi-colon is often optional.

So how does it decide when to assume an end-of-line?

Consider this example:

var x = 1 + 2
-3 + 3 == 0 ? alert('0') : alert('3')

Punching that into an HTML file and opening it in Safari popped up 3. If you stick a semicolon on the end of the first line, it changes to 0.

The algorithms and logic are all straightforward; what interests me is by what criteria JavaScript decided, in this instance, not to assume an end-of-line after the first line. Is it that it only waits for an error scenario before assuming an EOL? Or is there a more definite criteria?

I’m very curious. I haven’t researched this much; I want to see what the S/O community has to say about it. I always end my lines with semicolons anyway, but I have some JS compression code that trips on the semicolon issue from time to time when I inadvertently leave one out.

Edit

OK just to clarify what the actual question is here: Can anybody describe, in non-abstract terms, when JavaScript will and won’t automatically insert semicolons.

This is NOT a duplicate. I’m aware that the rules for automatic semicolon insertion are well established and concisely documented. They’re also long winded and confusing because they are generally abstract. In my experience, high level programmers don’t digest low level documentation as well as simple end results, which is what I’m looking for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:14:28+00:00Added an answer on May 22, 2026 at 9:14 pm

    The ECMA specification (ch. 7.9.1, page 26) states:

    There are three basic rules of
    semicolon insertion:

    1. When, as the program is parsed from left to right, 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:
      • The
      offending token is separated from the
      previous token by at least one
      LineTerminator.
      • The offending token
      is }.
    2. When, as the program is parsed from left to right, 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 ECMAScript Program, then a
      semicolon is automatically inserted at
      the end of the input stream.
    3. When, as the program is parsed from left to right, a token is encountered
      that 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
      restricted production (and therefore
      such a token is called a restricted
      token), and the restricted token is
      separated from the previous token by
      at least one LineTerminator, then a
      semicolon is automatically inserted
      before the restricted token.

    I think this implementation has to do with the second point where:

    var x = 1 + 2
    -3 + 3 == 0 ? alert('0') : alert('3')
    

    can be parsed as a single complete ECMAScript Program

    Because it’s not always clear how the parser will insert semi-colons, it’s advisable to not leave it to the parser (i.e. always insert the semi-colons yourself).

    In ch. 7.9.2 (Examples of Automatic Semicolon Insertion) of the same specs this example looks like your situation:

    The source

    a = b + c  
    (d + e).print()
    

    is not transformed by automatic
    semicolon insertion, because the
    parenthesised expression that begins
    the second line can be interpreted as
    an argument list for a function call:
    a = b + c(d + e).print()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What are the rules for Javascript's automatic semicolon insertion? I know you
Possible Duplicate: What are the rules for Javascript's automatic semicolon insertion? I read an
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: For what reason margin collapse rules were introduced in CSS? What is
Possible Duplicate: Changing a CSS rule-set from Javascript Dear experts, Is there a way
Possible Duplicate: Css and javascript Not Loading in rewrite rule After using only rewrite
Possible Duplicate: What is a computer programming language? No, really. I've struggled to come
Possible Duplicate: javascript compare strings without being case sensitive. I am having two fields.
Possible Duplicate: What should I do to obtain javascript intellisense for my own js
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.