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

Jiten Mehta
Jiten Mehta
Front End Web Development Techdegree Student 6,111 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?


2 Answers

Steven Parker
Steven Parker
160,421 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.