Michael Winter wrote:
Richard Cornford wrote:
<snip> The existence of the EmptyStatment - ; - (which is
harmless in most contexts) and automatic semicolon
insertion allows script authors to get away with
having little understanding of when and where
semicolons are necessary in javascript. [...]
I'd disagree with the former, and agree with the latter.
Empty statements are a part of other languages, including
compiled ones, which have no qualms over enforcing strict
adherence to the grammar.
<snip> while( condition );
<snip>
I have nothing against empty statments, and would prefer using one in an
IterationStatement where all the work was done in the control expression
(rather than an empty block).
But having empty statements allows them to be inserted where semicolons
would not otherwise be necessary (you would get some sort of error if
they did not exist), and that is true of compiled languages as well,
except in compiled languages you don't get away with omitting the
necessary semicolons so you end up learning where they need to be very
quickly.
The OP's question relates to an unnecessary EmptyStatement rather than
the missing semicolon at the end of the ReturnStatement (which is fixed
by automatic semicolon insertion). An example, seeing:-
var x = function(){
... // function body
};
- which is correct because the assignment expression becomes an
ExpressionStatement and should be semicolon terminated, someone might
infer:-
function x(){
... // function body
};
- where the semicolon is an EmptyStatement independent of the function
declaration. But getting away with that may serve to blur the
distinction between a function declaration and a function expression as
a side effect.
Richard.