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 Build a Quiz Challenge, Part 2 Solution

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Why in the print function,don't have return.But in the build list function have the return.

I don't quit sure when to use return?

2 Answers

Ace Motanya
Ace Motanya
31,756 Points

When the function is generating some value and you want to pass it back to the caller is when you want to use return.

If you want to store the return value, then you have to assign it to a variable

var value = someFunction();

This stores the return value of someFunction in value.

If you call the function without assigning the return value, then the value is just silently dropped like so:

someFunction();

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Did you mean that when we call the function name,it doesn't have the value inside,we need to use return to pass the value to the function name right?

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Can you explain what did this mean

var value = someFunction();

This stores the return value of someFunction in value.

If you call the function without assigning the return value, then the value is just silently dropped like so:

someFunction();

I am also learning but I think the print function did not need a return because the div id = 'output' did it instead on the HTML page. Look at output and DOM (.getElementByID) on Mozilla Developer Network. It got confusing when he changed it up a little bit but I think he said there are courses that go more in depth here on TreeHouse. Hope that helps.