semicolon

Discussion in 'Javascript' started by fritz, May 7, 2005.

  1. fritz

    fritz Guest

    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.
    fritz, May 7, 2005
    #1
    1. Advertising

  2. fritz wrote:


    > 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.


    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/
    Martin Honnen, May 7, 2005
    #2
    1. Advertising

  3. fritz

    fritz Guest

    Martin Honnen wrote:

    > 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.
    >
    >

    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.
    fritz, May 7, 2005
    #3
  4. fritz wrote:

    > Has anyone tried to take javascript up to
    > a general purpose language such as perl?


    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/
    Martin Honnen, May 7, 2005
    #4
  5. fritz

    Roger Guest

    fritz wrote:
    > 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.



    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
    Roger, May 7, 2005
    #5
  6. fritz

    fritz Guest

    Roger wrote:

    > 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


    I just visited the site and I will study it carefully in the next few days.

    >
    > 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/
    >


    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.

    > 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
    >

    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.
    fritz, May 7, 2005
    #6
  7. fritz

    fritz Guest

    Martin Honnen wrote:

    >
    > 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.



    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.
    fritz, May 7, 2005
    #7
  8. fritz wrote:

    > In other words using ASP scripting to assemble the page prior to sending
    > it since the client may have JS turned off?


    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.

    > Since ASP takes VBScript or JavaScript does it have to run on Windows
    > servers or can one run it on any?


    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
    David Dorward, May 7, 2005
    #8
  9. fritz

    fritz Guest

    David Dorward wrote:
    > fritz wrote:
    >
    >
    >>In other words using ASP scripting to assemble the page prior to sending
    >>it since the client may have JS turned off?

    >
    >
    > 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.
    >

    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.

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

    >
    >
    > 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).
    >

    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.
    fritz, May 7, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    3
    Views:
    4,595
  2. Russ

    orgin of semicolon usage

    Russ, Oct 24, 2003, in forum: C Programming
    Replies:
    9
    Views:
    497
    Derk Gwen
    Oct 26, 2003
  3. ankursinha
    Replies:
    33
    Views:
    22,323
    Keith Thompson
    Feb 25, 2004
  4. Prashanth Badabagni

    Printing "hello , wolrd" with out using semicolon

    Prashanth Badabagni, May 3, 2004, in forum: C Programming
    Replies:
    42
    Views:
    2,114
    Peter Nilsson
    May 12, 2004
  5. Peter Michaux

    Cookies: semicolon vs. semicolon-space

    Peter Michaux, Dec 10, 2006, in forum: Javascript
    Replies:
    3
    Views:
    217
    Michael Winter
    Dec 11, 2006
Loading...

Share This Page