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 The Refactor Challenge

initialExpression

Hi Guys, I have been able to complete the challenge can someone explain in more details about the initialExpression in for loops and the purpose behind it.

Thanks

1 Answer

Nick Welch
Nick Welch
5,725 Points

I just want to confirm what you mean by initialExpression. for example consider this loop.

for (var i = 1; i <= 10; i += 1) { //do something }

for defines the for loop. var i = 1 creates a variable named i and initializes it's value to 1. We use this as a counter as the loop executes. i <= 10 is the condition for the loop to keep running. In this case it will only keep running if our counter, i, is less than or equal to 10. i += 1 increments the counter each time it runs. This way the loop will eventually stop running.

Is this what you were needing more help with? If so I hope my response helped to clarify things. If you need additional help let me know.