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

Stivan Radev
Stivan Radev
1,475 Points

i<employees.length - JS Question

I'm currently doing the "Processing JSON Data" from the AJAX Basic course and I came upon this:

for (var i=0; i<employees.length; i+=1 )

Now I do understand what

var i = 0 

is and I also understand what

i += 1 

does, but the middle part

i<employees.length 

I kind of do not understand. Can somebody explain to me what is the middle part and what it is doing in the code

for (var i=0; i<employees.length; i+=1) 

Thanks!

1 Answer

Brendon Butler
Brendon Butler
4,254 Points

The middle part is the conditional. Basically, it checks to see whether or not a statement is true, then continues to loop until the statement is false.

Let's say there's 5 employees. You start out by setting your iterator to 0. You check to see whether 0 is less than the amount of employees (5 in this case). Then, it will execute any code within the loop and add one and set "i" equal to 1 more than its current value. It will loop until your variable is equal to or greater than the size of your array (5 employees). At which point it will break out of the loop. See Here

You can use i++ instead of i += 1 since it does the same thing. See Here

Stivan Radev
Stivan Radev
1,475 Points

Thanks man. Now I get it :)