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 Tracking Multiple Items with Arrays Using For Loops with Arrays

Volodymyr Boretskyi
Volodymyr Boretskyi
9,094 Points

clarify + list[i] +

can someone clarify please this + list[i] + when we build listHTML += '<li>' + list[i] + '</li>'; ?

Thanks

2 Answers

liktid
liktid
6,387 Points

This means that "list[i]" is an array within a loop. The + is a concatenation operator, which "connects" - in this case - the html <li>-tags with the array. While "i" is the variable with the value of the current loop (e.g. 1 or 2 or 3 depending on the condition) is "list" referring to the array.

Example:

var arrayExample = [arrayItem1, arrayItem2, arrayItem3];

list[i] is now looping through the array with i changing its value in every round.

list[0] = arrayItem1;
list[1] = arrayItem2;
list[2] = arrayItem3;

With the concatenation operator it would look like:

<li>arrayItem1</li>
<li>arrayItem2</li>
<li>arrayItem3</li>

I hope this helps.