Semi-Colon

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis M. Marks

    Semi-Colon

    I am never sure of when a semi-colon is required in javascript. Is
    there a definite rule?

    --
    Dennis M. Marks


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
  • Lasse Reichstein Nielsen

    #2
    Re: Semi-Colon

    "Dennis M. Marks" <denmarks@dcsi. net> writes:
    [color=blue]
    > I am never sure of when a semi-colon is required in javascript. Is
    > there a definite rule?[/color]

    Yes. Not a *simple* rule, but it is definitly there. The appropriate
    standard is ECMA 262. The relevant section is 7.9, "Automatic
    Semicolon Insertion" (which means that semicolons are required, but
    the compiler must insert them if they are missing).

    The short summary: A required statement-ending semicolon can be omitted
    if
    - it is just before a newline (or after, but it is inserted before),
    a block-end-bracket, "}", or the end of the program, and
    - there is no doubt that a semicolon should be there, i.e.,
    removing it doesn't give a syntactically correct program.
    That includes cases like
    foo();
    bar();
    (removing the first semicolon would not make it one statement),
    and six special cases where a newline isn't allowed:
    - between an expression and a following ++ or --,
    - between "continue" or "break" and a following identifier, or
    - between "return" or "throw" and a following expression.


    Cases to avoid are where the program makes sense without the semicolon:
    ---
    function foo(x){return x+42;}

    foo; // omit this ; and get another result.
    (5)
    ---

    I recommend always using semicolons to end your statements. It is never
    wrong, and it is *much* easier to remember.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Douglas Crockford

      #3
      Re: Semi-Colon

      > I am never sure of when a semi-colon is required in javascript. Is[color=blue]
      > there a definite rule?[/color]

      A semi-colon should be placed at the end of every statement. ECMAScript has a
      semi-colon insertion rule: In the face of some syntax errors, it will replace a
      linefeed with a semi-colon and retry. Professional programmers are troubled by
      this.

      jslint is a program that will help you to avoid semi-colon errors.



      Comment

      Working...