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 The Solution

Is there a clear advantage to returning a variable declared within a function?

Is there a clear advantage to declaring a variable inside a function that gets the content of the function and then returning that variable vs writing return before the content of the function and not declaring any variable?

Am I correct in assuming the second method will work fine in a simple function, where declaring variables within a function is necessary when the function is more complicated?

Thanks in advance!

3 Answers

Good question. One reason I CAN see to declare the variable is to make it clear what the function is doing/returning. Helps you and other developers reading the code later on. Code comments work too though!

No there isn't. In fact, my IDE will tell me i've declared an unnecessary variable if i can just return the content.

Thank you!

No there is not.