Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Functions Create Reusable Code with Functions Return a Value from a Function

Peter Huang
Peter Huang
3,838 Points

Why use return within function?

I've watched video over and over and I don't understand why even need to use a return within function.

Without return, it still work same way so why the extra work?

1 Answer

Steven Parker
Steven Parker
218,692 Points

There are two reasons to use "return" in a function:

  • if you want to pass a value back to the caller
  • if you want the function to end before all the code is executed

If you don't need either of these, it's perfectly proper to omit a "return" statement.