"Dennis M. Marks" <de******@dcsi.net> writes:
I am never sure of when a semi-colon is required in javascript. Is
there a definite rule?
Yes. Not a *simple* rule, but it is definitly there. The appropriate
standard is ECMA 262. The relevant section is 7.9, "Automatic
Semicolon Insertion" (which means that semicolons are required, but
the compiler must insert them if they are missing).
The short summary: A required statement-ending semicolon can be omitted
if
- it is just before a newline (or after, but it is inserted before),
a block-end-bracket, "}", or the end of the program, and
- there is no doubt that a semicolon should be there, i.e.,
removing it doesn't give a syntactically correct program.
That includes cases like
foo();
bar();
(removing the first semicolon would not make it one statement),
and six special cases where a newline isn't allowed:
- between an expression and a following ++ or --,
- between "continue" or "break" and a following identifier, or
- between "return" or "throw" and a following expression.
Cases to avoid are where the program makes sense without the semicolon:
---
function foo(x){return x+42;}
foo; // omit this ; and get another result.
(5)
---
I recommend always using semicolons to end your statements. It is never
wrong, and it is *much* easier to remember.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'