Reputation: 1200
I'm curious why when making a for loop in JavaScript a ; is not necessary after the i++ statement? It tripped me up. It actually doesn't even work if you include a semicolon after the iterator statement.
e.g.
for(var x=1; x<100; x++){
document.write(x);
}
Upvotes: 0
Views: 103
Reputation: 2904
While tokenizing the code for compiling, the JS-parser usually knows where statements start and end due to Automatic Semicolon Insertion.
In the case of a for-loop however, semicolons are interpreted as a separator of statements. For-Loops accept up to three "parameters". If you put in a semicolon, a fourth one will be expected and it will throw an error.
The final-expression-statement of a for-loop is terminated by )
.
This control-structure behaves similar to PHP, C, Java and C++
thanks to @Antoniu Livadariu for mentioning this in the comments
Further Information
Upvotes: 1