-1
for(i=0; i<=10; i++);
{
    document.writeln(i);
}

java script is ignoring the braces and printing 11. Why I'm not getting compiler error.

1 Answers1

5

A for loop can be expressed as either:

for () ... ;

or

for () { ... ; ... ; }

You've picked the former, except you do nothing between the conditions and the ; that ends the expression which gets evaluated each time you loop.

Then { ... } is a block. There's no reason for it to be a block because you don't do anything inside it at block level (like use let).

document.writeln(i) writes out the current value of i, which is 11 because that is the value i hit before it didn't meet the condition i <= 10.


For the block to be associated with the for loop you must not have a ; before it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335