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

why do you need i< array.length condition for javascript for loops

why do you need i< array.length condition for javascript for loops,

i mean lets say you have 10 items inside an array, i is gonna be less than 10(length) always, theres no way it will be larger than 10, so whats the purpose for this syntax anyway?

Thanks

2 Answers

A JavaScript for loop usually consists of three parts:

  1. The initial statement (executed before the code block.)
  2. the condition for the loop.
  3. A statement that runs after each iteration of the loop.

The "i<array.length" you mention is the second part, the condition. As long as this statement is true, the code inside the code block of the loop will run.

Let's look at a classic counter loop:

for (var i = 0; i < 10; i++) {
  console.log(i);
}

Part 1:

var i = 0;

This is the initial statement of the for loop. It runs before the loop begins. Here, it establishes a new variable, i, with a value of 0.

Part 2:

i < 10;

This is the conditional statement of the loop. The loop will execute the code block as long as this statement is true.

Part 3:

i++;

This statement runs after each iteration of the loop. Here, it increases the value of the i variable by one after each iteration. In the example above, the value of i will be logged to the console 10 times (0 - 9).

The second part, the condition, is not optional. It is the part of the for loop that evaluates to true or false, so it establishes when to start and stop running the code block of the loop. Without it, the code would run infinitely, because it would always evaluate as true! Technically, you could use a break statement inside of the code block at some point to end the loop, and there are instances where that might be necessary.

I hope this explanation helped. There are many types of loops available for iterating in JavaScript (including the for in and for of loops introduced in the more recent ECMAScript releases), but this is how a classic for loop works. And it works well!

Happy coding.

so basically the condition acts as a break statement after the last item in an array is loop through

there are arrays that you will want to .push() to, or that you want to to .pop() from, that you want to .splice() or .slice() or do all kinds of stuff with. If you would just alsways use the number of the length of your array as a number, then you have to manually keep track of when there are how many elements in your array. There might even be cases where you don't even know how long your array is in every single instance you call the loop.

That would result in a maintaining nightmare. It's generally best practice automate everywhere you can instead of manually putting in values and data.