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

Claire Kao
Claire Kao
4,393 Points

Declaring variable outside of the function

Can I render the items variable outside of the function? I've tried it myself. The result looks the same. Wonder what's the different having the item inside/outside the function. Thanks!

1 Answer

Steven Parker
Steven Parker
229,771 Points

The function will perform exactly the same if items is created in the global space, but since it is only used inside the function it is better practice to create it there. That way, after the function is finished, that variable can be disposed by the system and the space reclaimed instead of it continuing to take up space and not be used.