semicolon

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

    #1

    semicolon

    Hi,
    When I look through the "javascript bible" I don't find any example
    scripts that have lines with ending semicolons. However when I peruse
    this newsgroup I find that sometimes there are ending semicolons and
    sometimes not.

    Perhaps someone could enlighten me as to their proper usage in
    javascript.

    thank you

    f.
  • Martin Honnen

    #2
    Re: semicolon



    fritz wrote:

    [color=blue]
    > When I look through the "javascript bible" I don't find any example
    > scripts that have lines with ending semicolons. However when I peruse
    > this newsgroup I find that sometimes there are ending semicolons and
    > sometimes not.
    >
    > Perhaps someone could enlighten me as to their proper usage in
    > javascript.[/color]

    In JavaScript a semicolon terminates a statement, much the same as in
    other C-like languages. However one of the scripting language like
    features of JavaScript is an automatic semicolon insertion to free
    scripters from having to write it explictly. Thus while in C or Java you
    have to write

    statement1;
    statement2;

    in JavaScript you are free to write

    statement1
    statement2

    You should however be careful about statements like return or throw as
    an automaticaly inserted semicolon can hurt you and change the meaning
    of your script e.g. if you code
    return
    expression
    then due to the rules of automatic semicolon insertion that is parsed as
    return;
    expression
    and the return statement then does not return the result of evaluating
    the expression but the value undefined.


    The ECMAScript standard which standardizes the core JavaScript language
    gives the following rules of practical advice to scripters to avoid
    problems with automatically inserted semicolons:

    • A postfix ++ or -- operator should appear on the same line as its operand.
    • An Expression in a return or throw statement should start on the same
    line as the return or
    throw token.
    • A label in a break or continue statement should be on the same line as
    the break or
    continue token.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • fritz

      #3
      Re: semicolon

      Martin Honnen wrote:
      [color=blue]
      > In JavaScript a semicolon terminates a statement, much the same as in
      > other C-like languages. However one of the scripting language like
      > features of JavaScript is an automatic semicolon insertion to free
      > scripters from having to write it explictly. Thus while in C or Java you
      > have to write
      >
      > statement1;
      > statement2;
      >
      > in JavaScript you are free to write
      >
      > statement1
      > statement2
      >
      > You should however be careful about statements like return or throw as
      > an automaticaly inserted semicolon can hurt you and change the meaning
      > of your script e.g. if you code
      > return
      > expression
      > then due to the rules of automatic semicolon insertion that is parsed as
      > return;
      > expression
      > and the return statement then does not return the result of evaluating
      > the expression but the value undefined.
      >
      >
      > The ECMAScript standard which standardizes the core JavaScript language
      > gives the following rules of practical advice to scripters to avoid
      > problems with automatically inserted semicolons:
      >
      > • A postfix ++ or -- operator should appear on the same line as its
      > operand.
      > • An Expression in a return or throw statement should start on the same
      > line as the return or
      > throw token.
      > • A label in a break or continue statement should be on the same line as
      > the break or
      > continue token.
      >
      >[/color]
      Thank you very much Martin! I just downloaded the ECMAScript262 and
      will have a go at it. I take it then that terminating each line is up to
      the writer and doing it makes no difference in the long run except for
      things like 'return' or break. So if the language sees an ending ';' it
      doesn't add another.

      BTW, I googled on 'javascript IDE' and came up with not too much
      but a few references back in 2000. Has anyone tried to take javascript
      up to a general purpose language such as perl?

      f.

      Comment

      • Martin Honnen

        #4
        Re: semicolon



        fritz wrote:
        [color=blue]
        > Has anyone tried to take javascript up to
        > a general purpose language such as perl?[/color]

        J(ava)Script is being used in other environments as the browser, on
        Windows you can use it for automation with Windows Script Host and for
        server side scripting within ASP pages.
        Another example of automation using JavaScript is <http://www.jsdb.org/>.

        While those uses don't make JavaScript a general purpose programming
        language it certainly shows that the language is being used outside of
        the browser.



        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        • Roger

          #5
          Re: semicolon

          fritz wrote:[color=blue]
          > Hi,
          > When I look through the "javascript bible" I don't find any example
          > scripts that have lines with ending semicolons. However when I peruse
          > this newsgroup I find that sometimes there are ending semicolons and
          > sometimes not.
          >
          > Perhaps someone could enlighten me as to their proper usage in
          > javascript.
          >
          > thank you
          >
          > f.[/color]


          I found the advise about when to use/not use semicolons very confusing
          as well. I now run all my code through JSLint which has a much stricter
          set of coding rules than the Javascript language. JSLint demands that
          you always use semicolons. You can read about it here:


          If you are a Mozilla Firefox user, another under-utilized tool is JS
          Console that installs as a Firefox extension. JS Console is a
          development tool for writing complex Javascript routines. It encourages
          you to follow the principles of Extreme Programming -- you write a
          simple test to prove your code works, then you write enough code to pass
          the test, then you write a more complex test, then you write enough code
          to pass the test... repeating the process until you have working code.
          You can read more about it here:


          Both of the above will require some study before you will understand how
          to use them. They are good tools and well worth the effort.

          Roger

          Comment

          • fritz

            #6
            Re: semicolon

            Roger wrote:
            [color=blue]
            > I found the advise about when to use/not use semicolons very confusing
            > as well. I now run all my code through JSLint which has a much stricter
            > set of coding rules than the Javascript language. JSLint demands that
            > you always use semicolons. You can read about it here:
            > http://www.crockford.com/javascript/lint.html[/color]

            I just visited the site and I will study it carefully in the next few days.
            [color=blue]
            >
            > If you are a Mozilla Firefox user, another under-utilized tool is JS
            > Console that installs as a Firefox extension. JS Console is a
            > development tool for writing complex Javascript routines. It encourages
            > you to follow the principles of Extreme Programming -- you write a
            > simple test to prove your code works, then you write enough code to pass
            > the test, then you write a more complex test, then you write enough code
            > to pass the test... repeating the process until you have working code.
            > You can read more about it here:
            > http://jsconsole.mozdev.org/
            >[/color]

            As a matter of fact I do use the firefox browser as the primary. I see
            it under the tools menu. I just clicked upon it and found that it gives
            quite a lot of information. However, I didn't install it and it isn't
            listed under the extensions but there it is. Now to gradually use it also.
            [color=blue]
            > Both of the above will require some study before you will understand how
            > to use them. They are good tools and well worth the effort.
            >
            > Roger
            >[/color]
            Thank you so much Roger. It is nice to be able to ask for help and get
            such competent answers. I will try to do as you and others have suggested.

            f.

            Comment

            • fritz

              #7
              Re: semicolon

              Martin Honnen wrote:
              [color=blue]
              >
              > J(ava)Script is being used in other environments as the browser, on
              > Windows you can use it for automation with Windows Script Host and for
              > server side scripting within ASP pages.
              > Another example of automation using JavaScript is <http://www.jsdb.org/>.
              >
              > While those uses don't make JavaScript a general purpose programming
              > language it certainly shows that the language is being used outside of
              > the browser.[/color]


              I just took a look at the link jsdb. I am always amazed at how someone
              could do what he has done.
              Also, looked up ASP and it looks like that is something that someone
              referred to in a previous post:

              *************** ******
              Mr. Oz wrote:
              The type of script that you are suggesting is often run at the server to
              ensure that what is sent to the user is HTML and has no client-side
              dependencies outside support for stated the DOCTYPE.
              *************** ****

              In other words using ASP scripting to assemble the page prior to sending
              it since the client may have JS turned off? Since ASP takes VBScript or
              JavaScript does it have to run on Windows servers or can one run it on any?

              thanks

              f.

              Comment

              • fritz

                #8
                Re: semicolon

                Roger wrote:[color=blue]
                > I found the advise about when to use/not use semicolons very confusing
                > as well. I now run all my code through JSLint which has a much stricter
                > set of coding rules than the Javascript language. JSLint demands that
                > you always use semicolons. You can read about it here:
                > http://www.crockford.com/javascript/lint.html
                >
                > If you are a Mozilla Firefox user, another under-utilized tool is JS
                > Console that installs as a Firefox extension. JS Console is a
                > development tool for writing complex Javascript routines. It encourages
                > you to follow the principles of Extreme Programming -- you write a
                > simple test to prove your code works, then you write enough code to pass
                > the test, then you write a more complex test, then you write enough code
                > to pass the test... repeating the process until you have working code.
                > You can read more about it here:
                > http://jsconsole.mozdev.org/
                >
                > Both of the above will require some study before you will understand how
                > to use them. They are good tools and well worth the effort.
                >
                > Roger
                >[/color]
                It looks like my response to your post didn't make the grade. Blank.
                Anyway I downloaded the JSLint and in a few days after making my small
                code work properly I'll submit to it.

                AS to the JSConsole it appears to be installed in my firefox which I
                do use as the primary browser. I will use it from now on. It had a fit
                when I showed it a rather popular internet site. :-)

                Thanks

                f.

                Comment

                • David Dorward

                  #9
                  Re: semicolon

                  fritz wrote:
                  [color=blue]
                  > In other words using ASP scripting to assemble the page prior to sending
                  > it since the client may have JS turned off?[/color]

                  It is a good idea to use client-side JavaScript only for optional
                  enhancements (i.e. things that the user can live without) that degrade
                  gracefully.

                  ASP is one option, although not my first choice, for generating content
                  pragmatically before presenting it to the user.
                  [color=blue]
                  > Since ASP takes VBScript or JavaScript does it have to run on Windows
                  > servers or can one run it on any?[/color]

                  As well as VBScript and JScript, you can use PerlScript with ASP. There are
                  probably other languages available for it too, but my experience with it
                  isn't that great.

                  ASP can be used on non-windows platforms, but the languages you can use with
                  it will probably vary (and you won't be able to use Windows specific
                  components).

                  --
                  David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
                  Home is where the ~/.bashrc is

                  Comment

                  • Roger

                    #10
                    Re: semicolon

                    fritz wrote:[color=blue]
                    > Roger wrote:[color=green]
                    > > I found the advise about when to use/not use semicolons very confusing[/color]
                    >[color=green]
                    >> as well. I now run all my code through JSLint which has a much
                    >> stricter set of coding rules than the Javascript language. JSLint
                    >> demands that you always use semicolons. You can read about it here:
                    >> http://www.crockford.com/javascript/lint.html
                    >>
                    >> If you are a Mozilla Firefox user, another under-utilized tool is JS
                    >> Console that installs as a Firefox extension. JS Console is a
                    >> development tool for writing complex Javascript routines. It
                    >> encourages you to follow the principles of Extreme Programming -- you
                    >> write a simple test to prove your code works, then you write enough
                    >> code to pass the test, then you write a more complex test, then you
                    >> write enough code to pass the test... repeating the process until you
                    >> have working code. You can read more about it here:
                    >> http://jsconsole.mozdev.org/
                    >>
                    >> Both of the above will require some study before you will understand
                    >> how to use them. They are good tools and well worth the effort.
                    >>
                    >> Roger
                    >>[/color]
                    > It looks like my response to your post didn't make the grade. Blank.
                    > Anyway I downloaded the JSLint and in a few days after making my small
                    > code work properly I'll submit to it.
                    >
                    > AS to the JSConsole it appears to be installed in my firefox which I
                    > do use as the primary browser. I will use it from now on. It had a fit
                    > when I showed it a rather popular internet site. :-)
                    >
                    > Thanks
                    >
                    > f.[/color]

                    You probably have "Javascript Console" installed on Firefox, which is
                    different from JS Console. "Javascript Console" is pretty handy as well
                    and tells you about errors you have on the current web page. "Javascript
                    Console" is installed by default.

                    "JS Console" is not installed by default, you have to install it
                    yourself. JS Console does not know about web pages, it is a heavy duty
                    development tool for writing complex Javascript procedures.

                    Since the thread is now about Firefox, there is yet another great
                    extension for validating your HTML code. It is called the Firefox HTML
                    Validator. You can read about it at:



                    Roger

                    Comment

                    • fritz

                      #11
                      Re: semicolon

                      David Dorward wrote:[color=blue]
                      > fritz wrote:
                      >
                      >[color=green]
                      >>In other words using ASP scripting to assemble the page prior to sending
                      >>it since the client may have JS turned off?[/color]
                      >
                      >
                      > It is a good idea to use client-side JavaScript only for optional
                      > enhancements (i.e. things that the user can live without) that degrade
                      > gracefully.
                      >[/color]
                      After all of the comments that I have gotten here, I think that my
                      course of action will be: Writing the menu and positioning with
                      javascript and css, and then only after all is working well, start the
                      exploration of the other ways of doing it.
                      [color=blue]
                      > ASP is one option, although not my first choice, for generating content
                      > pragmatically before presenting it to the user.
                      >
                      >[color=green]
                      >>Since ASP takes VBScript or JavaScript does it have to run on Windows
                      >>servers or can one run it on any?[/color]
                      >
                      >
                      > As well as VBScript and JScript, you can use PerlScript with ASP. There are
                      > probably other languages available for it too, but my experience with it
                      > isn't that great.
                      >
                      > ASP can be used on non-windows platforms, but the languages you can use with
                      > it will probably vary (and you won't be able to use Windows specific
                      > components).
                      >[/color]
                      Thank you for the comments Mr. Dorward! I peeked at your site and
                      now I have more tutorials to go through ;-) Where does the time go?
                      Thanks again.

                      f.

                      Comment

                      • fritz

                        #12
                        Re: semicolon

                        Roger wrote:
                        probably have "Javascript Console" installed on Firefox, which is[color=blue]
                        > different from JS Console. "Javascript Console" is pretty handy as well
                        > and tells you about errors you have on the current web page. "Javascript
                        > Console" is installed by default.
                        >
                        > "JS Console" is not installed by default, you have to install it
                        > yourself. JS Console does not know about web pages, it is a heavy duty
                        > development tool for writing complex Javascript procedures.
                        >
                        > Since the thread is now about Firefox, there is yet another great
                        > extension for validating your HTML code. It is called the Firefox HTML
                        > Validator. You can read about it at:
                        >
                        > https://addons.update.mozilla.org/ex...cation=firefox
                        >
                        >
                        > Roger
                        >[/color]
                        You are correct. It isn't installed. I'll go get it and the
                        validator and work with it!
                        f.

                        Comment

                        Working...