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
Alex Watts
8,396 PointsThe return value.
Hello,
I am struggling to understand the concept of the return value. When can it be used and why.
For example, why does this code not need to use the return value?
var theReturn = function() {
var total = 10 * 10;
console.log(total);
}
theReturn();
Thanks!
1 Answer
Julian Gutierrez
19,325 PointsThe return statement is used anytime you need to "return" a value to the function caller. In the example you gave there is no need to return the solution of 10x10 because it is not used outside of the function. In the example below we run Bob's age through the addFive function so we can use that value in the alert outside of the function.
var age = 25;
alert("Bob will be " + addFive(age) + " in 5 years.");
function addFive(num){
return num+=5;
}