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.

Advertisements

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

Advertisements

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?

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.

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

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?

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

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!