I was having a look at some code a friend did and stumbled across this line which I thought was an error (simplified for example)..
for (g = 0; 10 > g; g+
As far as I know this is simply a syntax feature of the language. Basically, javascript ignores that semicolon.
EDIT: Sorry, misread your post. If you mean that you only get alerted once, then you have basically an empty loop executing ten times, then a single block of code containing the alert executing once.
Accolades ({...}) can go around any block of code, independent of constructs such as for-loops.
Your g
variable is within the global context, so it is accessible outside of a for
loop.
The loop did its job, and incremented g
10 times.
Two {}
indicate a block in JavaScript and won't cause any error.
Edit: it is not alerting anything without a for
loop, because the g
variable is not defined.
The semi colon actually runs the loop without the {} code block. So your code basically has 2 parts,
variable g
to from 0 to 10 in 10 steps,g
(now equals to 10) once.your for loop runs 10 times but wont alert anything since you have put ;
that alert you are getting is only once because of that statement outside of for loop