Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
215,958 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
215,958 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.