";" after if statement??

J

JS

In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s.length = 0;
opt = document.createElement('OPTION');
s.appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";" but I have seen other if statments that
do not end with that. Why is if statement sometimes ended by a ";".?
 
D

Deniz Adrian

JS said:
In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s.length = 0;
opt = document.createElement('OPTION');
s.appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";" but I have seen other if statments that
do not end with that. Why is if statement sometimes ended by a ";".?


I guess, the semicolon should be after "return true" and not after the
closing bracket.

best regards
Deniz
 
R

Richard Cornford

JS said:
In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s.length = 0;
opt = document.createElement('OPTION');
s.appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";"


No it isn't. The semicolon, following the closing brace that represents
the end of the Block statement that is the conditionally executed
statement of the IfStatement, is an EmptyStatment. Harmless but
unnecessary.
but I have seen other if statments
that do not end with that.

The IfStatement production does not require a terminating semicolon. If
the Statement ending the production - if ( Expression) Statement - is
not a Block statement then that statement may need to be terminated with
a semicolon, giving the impression that the IfStatment ends with a
semicolon.

ECMAScript performs 'automatic semicolon insertion' so under some
circumstances semicolons that are required by the production rules may
be omitted as the interpreter will insert them itself. See ECMA 262 3rd
edition section 7.9.
Why is if statement sometimes ended by a ";".?

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. That lack of understanding can be observed when
looking at example scripts.

Richard.
 
M

Michael Winter

On 27/05/2005 08:56, 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. I
can't think of enough examples to decide if empty statements are ever
necessary, but they can be used, for example, in iterative statements
where the condition also performs an action that renders the loop body
redundant:

while( condition );

However, this could be equally represented with:

while( condition ) {}

as the StatementList within a Block is optional. This is true in C (and
C++), and Java (the grammar summary for the latter is misprinted).

Mike
 
R

Richard Cornford

Michael said:
Richard Cornford wrote:
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.
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.
 
R

Richard Cornford

Richard Cornford wrote:
var x = function(){
... // function body
};

- which is correct because the assignment expression becomes
an ExpressionStatement and should be semicolon terminated, ...
<snip>

Actually the assignment expression becomes part of a VariableStatement,
which is also semicolon terminated. I probably should have left the -
var - keyword out.

Richard.
 
J

John G Harris

JS <[email protected]> said:
In this sample code:

if(n==0&&args>1){
for(i=num;args>i+1;i++){
s.length = 0;
opt = document.createElement('OPTION');
s.appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
};

the if statement is ended by a ";" but I have seen other if statments that
do not end with that. Why is if statement sometimes ended by a ";".?


It might help to understand other people's answers if the code is laid
out to show how the javascript parser will see it :

if (n==0 && args>1)
{
for (i=num; args>i+1; i++)
{
s.length = 0;
opt = document.createElement('OPTION');
s.appendChild(opt);
opt.value = "";
opt.text = "\74-- Vælg --";
}
return true
}

;

Each extra level of indenting marks a statement or statements nested
inside another statement. (The parser will also automatically add a
semicolon after 'true').

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top