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 For Loops

Philip Schultz
Philip Schultz
11,437 Points

Why isn't there a semicolon after the increment statement in the for loop?

Within the for loop we have three statements - We declare the variable, we set the condition and we add the increment to the variable. Why don't we put a semicolon at the end of the increment statement like the other two statement?

for ( var i = 1 ; i <= 10 ; i += 1 (why isn't there a semicolon here )){

}

1 Answer

Steven Parker
Steven Parker
229,787 Points

:point_right: The parts of a for loop aren't actually statements.

The for loop takes three optional expressions, enclosed in parentheses and separated by semicolons. Since the semicolons only separate the expressions from each other, only two are required.

A third one would be interpreted as a separator for a fourth (omitted) expression, which would not be proper syntax.