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 Arrays Loop Through Arrays Loop Through an Array

Why items = ''? why it starts emptyy?

?

2 Answers

Ignacio Rocha
Ignacio Rocha
7,462 Points

It start as an empty string because as the function runs, it will iterate in the for loop and in every iteration a new <li> element, with the value of the array in that location, will be created.

Hi Ignacio,

Thank you.

Do we have to declare an empty string every time when we run a function?

Ashley Schott
Ashley Schott
7,944 Points

Adding to Ignacio's answer, if I understand correctly, we don't want the scope of our items variable to be limited to inside the for loop, because we need to return items when we call the function. So we will initialize the variable as an empty string before the for loop. Where it is initialized and placed has to do with where and how we need to use it. And we can't initialize it to the template literal the instructor uses inside the for loop because we don't have i to call on yet.