A semi-colon explicitly placed in the source text must terminate certain statements. Your JavaScript interpreter may help by adding some automatically, but this may not work as you expect. As they say, "Your mileage may vary".
Semi-colons are used to explicitly terminate certain keywords so that the parser can determine exactly where the fragment of code begins and ends. The semi-colon removes the ambiguity about how a piece of code is intended to execute.
Line terminators greatly affect the automatic semi-colon insertion process.
The following statements must have trailing semi-colons:
empty statement
variable statement
expression statement
continue statement
break statement
return statement
throw statement
There are cases where the the interpreter will automatically insert semi-colons as needed. You won't see them in the script source, but the interpreter knows they should be there. You should not rely on the interpreter doing your work for you. For example, semi-colons are never added inside for statement headers. Here are some instances of how the browser deals with automatic semi-colon insertion:
Semi-colons are automatically placed before curly braces (}) that close code blocks if necessary.
A semi-colon is added at the end of a script source text if necessary to parse the source as a complete program.
Semi-colons are added to prevent accidental postfix increment or decrement operations. Postfix ++ or -- operators should be on the same line as the operand to which they apply. Actually it is good practice for there to be no white space between them.
Semi-colons are added after the return statement when it is the last statement on a line. An expression to be evaluated as part of a return statement should be placed adjacent to it. It is good practice to form the return as if it were a function, enclosing the expression in parentheses:
return(expression);
This is unaffected by automatic semi-colon insertion even though it is syntactically incorrect:
for (a; b )
This is transformed:
return a + b
And becomes:
return; a + b;
However, a + b is not returned as a result because the line terminator separates them from the return statement.
People take a great many liberties with the formatting of if then constructions. This won't get fixed:
if(a > b) else c = d
This won't get fixed either:
a = b + c (d + e).print()
It doesn't get fixed because the parentheses look like a function call. Thus:
a = b + c(d + e).print()
Line terminators greatly affect the automatic semi-colon insertion process.
Careful programmers always put semi-colons in. If you come from a C or Java background, this may be instinctive, but otherwise you should develop the habit so that it becomes instinctive.
ECMA 262 edition 2 - section - 7.8
ECMA 262 edition 3 - section - 7.9
O'Reilly JavaScript Definitive Guide - page - 28
Wrox Instant JavaScript - page - 17
Prev | Home | Next |
AuthentiCode | Up | Automation object |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |