JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops For Loops

Jiten Mehta
Jiten Mehta
Front End Web Development Techdegree Student 10,451 Points

There is no ' ; ' at the end of the condition in the for loop.

Hi guys, not sure if I have missed this in the video, but in the for loop, there is no semicolon to end the program in the condition. For example, initially, I put in the following.

for ( var i = 1; i <=10; i +=1; ) { html += ' <div> ' + i + '</div>' }

but then I got a syntax error in the console. It was then I noticed Dave didn't have a ' ; ' at the end of the condition. For example,

for ( var i = 1; i <=10; i +=1 ) { html += ' <div> ' + i + '</div>' }

I just wanted to know why this was?


3 Answers

Steven Parker
Steven Parker
194,132 Points

The correct syntax is to separate the three clauses of a "for" loop with semicolons. Note that these parts of the loop are expressions, and the semicolons serve as separators and are not terminators like they are for statements.

So there should always be only two semicolons.

Chris Mennens
Chris Mennens
3,638 Points

Jitan, Just to add a little to this, remember that Dave recommended the MDN for us to look up and keep in practice. I also had this very same question and got the answer going here:

MDN For Loop Documentation:

I really personally struggle with implementing things I learn like resources given so I have been trying to do better with things like this and actually put best practices to work.