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 Basics (Retired) Creating Reusable Code with Functions Getting Information From a Function

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

So the role of return is to store the value of a function?

Since the function in the last video can be written in a different way and deliver the same result, I suppose that return is for the situations when you want to store the value of the function?

1 Answer

Kristian Gausel
Kristian Gausel
14,661 Points

Return will collapse the current stack frame and push the result to the stack.

In normal english, it goes out of the function, and says that the value of calling the function is what you type after return.

For example:

function func(){
return 10;

Is a function that returns the value 10. You can call it like this, and the value will never be stored anywhere, NOTE: all sideeffects will are still happening, so this could be valid code (if the function has a sideeffect):


Or like this, and the value (10) will be stored in x:

var x = func();