Evertjan. said:
Lasse Reichstein Nielsen wrote on 17 feb 2011 in
comp.lang.javascript:
Defining a group of statements as statements is fine,
but that does not imply that that is the definition of statement.
True, it's not the only possible definition of Statement, but it *is*
the one that is assumed unless otherwise stated.
...
You are missing the point.
an if-statement is not the word if but the whole construct:
if (boolean) statement else statement;
... without the final ';'. It's not part of the if-statement.
A statement can contain statements and a statement can be a group of
statements in {}s.
so:
if (boolean) statement;
ends the statement, but:
It ends the if-statement before the ';', and adds an empty statement
after it.
It would also be ended by another statement, e.g.,
if (boolean) statement while(boolean) other_statement
if (boolean) statement
does not, as it expects either else or ;
No. It does fine without either, e.g.
{ if (boolean) statement }
or
if (boolean) statement var x = expression;
Well I can and I doo.
A statement ends when there is no more effect on the next statement.
I'm afraid that makes no sense to me. Probably because I don't grok
what you mean by effects on another statement.
Methinks maybe your preconsception that is wrong.
The notion of statement exists from the Algol days or even before
that, and used too be similar to a line of code. Later multiline
statements were conceived but the concept that when a statement ends
it has no influence on the next statement is still paramount.
So .... statements are separate and not overlapping?
In Javascript like languages
JavaScript took its grammar from Java, which took it from C. The only
thing JavaScript did different from Java (wrt. semicolons) was to
allow omission of some of them.
In Java
for(;
{};
is also two statements, the latter being empty, and
if (b) for(;
{}; else f();
is a syntax error (as John G Harris pointed out).
Also, the idea that semicolon is a statement terminator isn't
universal. In, e.g., Pascal, it's a statement *separator*, so you
write:
x := 2; y := 4
but with no semicolon at the end, and likewise
if (i = 0) then begin
x := 2;
y := 4
end
with no semicolong after "4" or "end".
the new line became less important and was
or could be replaced by the ; as an end of statement marker, [though
not completely so, I believe. Cannot find an example now.].
Example:
var x = 42
/foo/g.test("foo")
The rule in JavaScript is that a semicolon can be omitted if the
current statement could be ended at this point by a semicolon, there
is a newline or the next token is '}' or the end of input, and the
next token, if any, cannot possibly be a continuation of the current
statement.
Some statements have such an obvious end, that a ; or a newline is
not necessary, seen the error lenient nature of the parsing.
Not only is it not necessary, it's simply not part of the statement
grammar for those statements.
alert('blah'); is such an example.
Not really.
alert('blah')
(2+"x").substring(2)
fails because the semicolon is necessary, and newline isn't even
good enough.
But to sat that in that case the ; is an empty statement is just
conseptual end in my view conceptually wrong, as it is not carried
from the runtime parser to the runtime executer.
The grammar is what it is. You can disagree with the design, but
whether it's conceptually wrong or not, it is the way it is.
It seems to me that you have a preconcieved idea of how things should
be, and is now trying to fit the square ECMAScript grammar into a
round concept hole.
In my view then ; in alert('blah'); is conceptually necessary for a
complete, logical and optimally readable script. The fact thet the
error of leaving it out is forgiven by the parser does not make that
the standard.
Are you redefining the word "standard" now?
Allowing omission of the semicolon after alert('blah') (in some cases)
is part of the standard.
The reason to not use that part of the standard is that it doesn't aid
readability and the edge cases are so confuzing that a beginner
shouldn't have to figure it out. Putting in all *statement
terminating* semicolons is a both safe and correct approach. Adding
more semicolons than that is again error-prone because it is at odds
with how the language actually works.
So a complete statement could and should look like this:
if (boolean)
{
statement1;
statement2;
statement3;
}
else
{
statement4;
statement5;
statement6;
}
;
The if statement only ends at my last ;
No it doesn't. It ended at the previous '}'. The ';' is not
part of the if-statement in ECMAScript - independently of your
conceptual model.