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 trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

Anudeep Nayakoti
Anudeep Nayakoti
2,032 Points

why there is no semicolon to the final condition declared inside the 'For' loop?

for(var counter=0; counter<=10; counter+=10) { statement1; } -------xxx-------------------- var counter=0; while(counter<=10){ counter+=1;----->Here we are using semicolon; }

2 Answers

Steven Parker
Steven Parker
229,657 Points

The parts of a for loop are not statements.

A for statement has three sections, these are known as the initializer, condition, and iterator. They are enclosed in parentheses and separated by semicolons. These semicolons serve a different purpose than the ones you see at the end of statements.

If you were to put a semicolon after the iterator section, that would imply that there is a fourth (empty) section and would cause a syntax error.

Anudeep Nayakoti
Anudeep Nayakoti
2,032 Points

I agree. I did see the error; But inside the while loop, we do keep and the interpreter in the browser doesn't throw error;

Steven Parker
Steven Parker
229,657 Points

The semicolon you are referring to is in the body of the while loop. This is the same as the semicolon you show in the body of the for loop after "statement1".

A while statement has only one section (the condition), and there is no semicolon there.