newbe: where to put ";" in a script?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adrien

    newbe: where to put ";" in a script?

    Hi,

    i start with javascript and i saw a lot of examples, but sometimes there is
    ";" on each end of line, sometimes only certain lines, sometimes nowhere.
    So my question is: where must i put ";" in a javascript (in function, out
    function)?

    thanks for help
    ad


  • Douglas Crockford

    #2
    Re: newbe: where to put ";&quot ; in a script?

    > i start with javascript and i saw a lot of examples, but sometimes there is[color=blue]
    > ";" on each end of line, sometimes only certain lines, sometimes nowhere.
    > So my question is: where must i put ";" in a javascript (in function, out
    > function)?[/color]

    Put ';' at the end of all non-block statements. jslint will help you to place
    them properly.



    Comment

    • adrien

      #3
      Re: newbe: where to put ";&quot ; in a script?

      thanks

      "Douglas Crockford" <nospam@laserli nk.net> wrote in message
      news:bfjt0p$c0f $1@sun-news.laserlink. net...[color=blue][color=green]
      > > i start with javascript and i saw a lot of examples, but sometimes there[/color][/color]
      is[color=blue][color=green]
      > > ";" on each end of line, sometimes only certain lines, sometimes[/color][/color]
      nowhere.[color=blue][color=green]
      > > So my question is: where must i put ";" in a javascript (in function,[/color][/color]
      out[color=blue][color=green]
      > > function)?[/color]
      >
      > Put ';' at the end of all non-block statements. jslint will help you to[/color]
      place[color=blue]
      > them properly.
      >
      > http://www.crockford.com/javascript/lint.html
      >[/color]


      Comment

      • Dr John Stockton

        #4
        Re: newbe: where to put &quot;;&quot ; in a script?

        JRS: In article <bfjre9$sh3$1@r eader08.wxs.nl> , seen in
        news:comp.lang. javascript, adrien <hghjgh@sdgs.fb > posted at Tue, 22 Jul
        2003 19:08:42 :-
        [color=blue]
        >i start with javascript and i saw a lot of examples, but sometimes there is
        >";" on each end of line, sometimes only certain lines, sometimes nowhere.
        >So my question is: where must i put ";" in a javascript (in function, out
        >function)?[/color]


        (A) Where there are two or more separate statements on a single line,
        you must use a semicolon to separate them.


        (B1) At the end of a line, it is often necessary not to put a semi-
        colon, because the statement is not yet complete.

        (B2) At the end of a line, it is sometimes necessary to put a semi-colon
        to show that the statement is complete.

        (B3) Otherwise, at the end of a line, please yourself. Many people use
        them whenever possible.

        (B4) Note that two lines may be valid code, sometimes even meaningful
        code, but giving different results, both with and without the semi-
        colon. Consider
        for (j=0;j<2;j++) // possible ; here
        alert(j)


        A programmer should know, when about to start a new line, whether the
        previous statement is intended to finish. If it is, a semicolon is
        permissible and perhaps desirable. If it is not, it must be evidently
        incomplete, by having an unclosed bracket or a terminal operator (or
        ....?).


        The system will insert a semicolon (or act as if it has) when it sees
        fit; it will not remove one that it dislikes.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        • John G Harris

          #5
          Re: newbe: where to put &quot;;&quot ; in a script?

          In article <Xi5ZE0GQKaH$Ew dc@merlyn.demon .co.uk>, Dr John Stockton
          <spam@merlyn.de mon.co.uk> writes
          <snip>[color=blue]
          >(A) Where there are two or more separate statements on a single line,
          >you must use a semicolon to separate them.[/color]
          <snip>

          If you'd bothered to RTFM you wouldn't have written such misleading
          advice.

          In C-like languages *some* kinds of statement end with a semicolon
          (which in javascript can be omitted in some circumstances).
          e.g. a = 5; ++a;
          Other kinds don't end with a semicolon.
          e.g. { }

          The rule is different in Pascal-like languages where the semicolon is
          not part of the statement.

          John
          --
          John Harris
          mailto:john@jgh arris.demon.co. uk

          Comment

          • Douglas Crockford

            #6
            Re: newbe: where to put &quot;;&quot ; in a script?

            > >(A) Where there are two or more separate statements on a single line,[color=blue][color=green]
            > >you must use a semicolon to separate them.[/color][/color]
            [color=blue]
            > If you'd bothered to RTFM you wouldn't have written such misleading
            > advice.
            >
            > In C-like languages *some* kinds of statement end with a semicolon
            > (which in javascript can be omitted in some circumstances).
            > e.g. a = 5; ++a;
            > Other kinds don't end with a semicolon.
            > e.g. { }
            >
            > The rule is different in Pascal-like languages where the semicolon is
            > not part of the statement.[/color]

            I think you are being unfair here. JavaScript has a semicolon insertion
            mechanism which attempts to correct syntax errors by replacing linefeeds with
            semicolons. I think this was a bad idea. I don't trust it. I think that relying
            on it is strictly unprofessional. That said,

            Semicolon insertion does not work in the middle of a line. So if you put two
            statements on the same line, you must use the semicolon to separate them. That
            can look Pascal-like. That said,

            I think it is bad to put two statements on one line. It impairs readability and
            increases the likelihood of editing mistakes.



            Comment

            • Dr John Stockton

              #7
              Re: newbe: where to put &quot;;&quot ; in a script?

              JRS: In article <vDtUUSNMQuH$Ew Ye@jgharris.dem on.co.uk>, seen in
              news:comp.lang. javascript, John G Harris <john@nospam.de mon.co.uk>
              posted at Wed, 23 Jul 2003 20:37:48 :-[color=blue]
              >In article <Xi5ZE0GQKaH$Ew dc@merlyn.demon .co.uk>, Dr John Stockton
              ><spam@merlyn.d emon.co.uk> writes
              > <snip>[color=green]
              >>(A) Where there are two or more separate statements on a single line,
              >>you must use a semicolon to separate them.[/color]
              > <snip>
              >
              >If you'd bothered to RTFM you wouldn't have written such misleading
              >advice.
              >
              >In C-like languages *some* kinds of statement end with a semicolon
              >(which in javascript can be omitted in some circumstances).
              > e.g. a = 5; ++a;
              >Other kinds don't end with a semicolon.
              > e.g. { }[/color]

              Please show a circumstance where there are two or more separate
              statements on a single line, but a semicolon is not necessary to
              separate them.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: newbe: where to put &quot;;&quot ; in a script?

                Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
                [color=blue]
                > Please show a circumstance where there are two or more separate
                > statements on a single line, but a semicolon is not necessary to
                > separate them.[/color]

                if(x==4){x=2}y= 3

                A *reasonable* example would be harder.
                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • John G Harris

                  #9
                  Re: newbe: where to put &quot;;&quot ; in a script?

                  In article <bfmrhu$crs$1@s un-news.laserlink. net>, Douglas Crockford
                  <nospam@laserli nk.net> writes
                  <snip>[color=blue]
                  >Semicolon insertion does not work in the middle of a line. So if you put two
                  >statements on the same line, you must use the semicolon to separate them. That
                  >can look Pascal-like.[/color]

                  I think you've misunderstood my point. I'm pointing out that some kinds
                  of statement do not end in a semicolon, so the semicolon rules don't
                  apply to them. For instance,
                  { } ++a;
                  is a perfectly valid line containing two statements.
                  [color=blue]
                  >That said,
                  >I think it is bad to put two statements on one line. It impairs readability and
                  >increases the likelihood of editing mistakes.[/color]

                  I agree with you in general, but there are special cases where I think
                  one line is better :
                  <BODY onload="a=true; b=2;">

                  John
                  --
                  John Harris
                  mailto:john@jgh arris.demon.co. uk

                  Comment

                  Working...