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 is "return items" outside of the for loop?

I tried running the code with "return items" inside the for loop curly brackets, and only the first item on the list displayed on the page. Then I rewrote it with the return items outside the for loop (same as Guil's) and the full list displayed. Can someone explain to me why this happened?

1 Answer

Reggie Williams
STAFF
Reggie Williams
Treehouse Teacher

Esther AlQaisi este great work experimenting! Only one item is returned because the return statement ends the function. When placed outside, the entire loop can run before return is called.

I knew there was an answer I was missing! Thank you, that makes sense. And now I remember Guil mentioning that in a previous lesson.