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 Introduction to Programming Objects and Arrays Arrays

Tatiana Perry
Tatiana Perry
17,156 Points

Variables in js

var family = ["Tatiana", "Jarrett", "Darren", "Denise"]; for (var i=0; i<4 ;i+=1) console.log(family[i])

I am having trouble understanding how this works. If we first give the variable the name family and then in the for statement we use i, how does it know to use the array. Wouldn't it read something more like for( family=0...)

3 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

No. you're not assigning 'i' to family, you are cycling through the array and console.log each element at the location of i . with for (var i=0; i<4 ;i+=1) , you are saying: we are going to create a variable named 'i' and assign it to zero.

and while 'i' is less than 4 we are going to run some code.

and after we run that code, we are going to add one to i .

and each time you run that code you are logging family[whatever i is on this cycle {iteration} ] to the console.

So first time around i is going to equal 0. So you will log the item in the Family array that is at location 0..which is Tatiana. Then next time around i will equal 1 (0+1), so you will log Family[1] which is Jarrett.

make sense?

Tatiana Perry
Tatiana Perry
17,156 Points

That makes so much more sense!!!

Parag Nair
Parag Nair
2,269 Points

The for loop has 3 parts separated by ;

  1. Declaring placeholders/variables to use within the loop
  2. Condition to be satisfied to enter the loop
  3. What to do once the code inside the loop is executed

Where it says var i=0 you are just defining a new variable as a counter to access something within a predefined array Where it says i < 4, we are making sure we run the code within the for only 4 times (0,1,2,3) Where it says i += 1, we are just increasing the value of i so that by the time the loop has ran 4 times, the value of i becomes 4

When you do family = 0 you are actually assigning the value 0 to the variable family which would replace the whole array with a single value 0. What you need to do instead is just access an item in that family array which you would get by refering variable[index]. Since indexes are ZERO (0) based on javascript to access the first item you would need to use family[0]

so each time the condition is satisfied, the code enters the for loop and fetches the item from array family[i] so the first time it enters the loop it would be family[0] since the value of i is 0. Since the variable i is incremented every time the loop runs it will be refering to a different element within the array like

family[1] family[2] family[3]

Hope this is much clear.

Tatiana Perry
Tatiana Perry
17,156 Points

Thanks for explaining the parts of the for loop really well.