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/