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

Why do we need the return statement?

What effects would happen if I don't put one and why.

1 Answer

what is the function? the return statement returns the value computed by the function to the point in the code where the function was called, and also stops the running of the function. some functions create a 'side-effect' and if that is the purpose a return statement may not be necessary. for example, a function that changes the color of some text might not need to return anything. a return statement by itself can be used to stop and exit the function on a certain condition, if that is desired. like in a keydown event handler, if certain keys are pressed and you want nothing to happen, you can just return and the function will exit.