Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJiten Mehta
Front End Web Development Techdegree Graduate 21,209 PointsThere 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?
Thanks!
3 Answers
Steven Parker
231,236 PointsThe 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.
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 PointsHi Steve, thanks for the explanation. I understand now. Cheers!
Chris Mennens
4,710 PointsJitan, 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: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
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.