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 trialAlex Watts
8,396 PointsJava Script return.
Hello, I still do not understand the return value. Could someone explain what is does how it does what it does and why. Thanks!
5 Answers
Karolin Rafalski
11,368 PointsI will try an analogy.
Imagine a washing machine; it is a self contained unit that is full of water and detergent that specifically washes clothes.
You don't want the water and detergent anywhere but in that washing machine and you only want things that you specifically put in the washing machine to be washed.
Let's say you put a dirty t-shirt in the washing machine. At some point, it is done being washed and needs to be taken out of the washing machine and used somewhere else. Return works by taking out the (now clean) t-shirt from the washing machine and putting it somewhere else. That somewhere else is dependent on your code.
Alex Watts
8,396 PointsOK, so does this mean that once the return has been used that specific value is no longer contained in the function.
Karolin Rafalski
11,368 PointsI would recommend going to repl.it choose JavaScript and then try a simple function and include a console.log() to show you what is happening.
For example enter on repl.it on the left:
function addNumbers(a,b){
return a+b;}
then press run (button above where you entered this text). This loads the function to the right side
Now you'll be able to call the function by typing on the RIGHT side (where the command line is)
addNumbers (5,10)
and it should give back 15 (or you can choose whatever two numbers you'd like.
Then you can use add console.log to show you what is going on inside the function. Like so:
function addNumbers(a,b){
console.log("This text will appear");
console.log("the value of a is " + a);
console.log("the value of b is " + b);
return a+b
console.log ("This text w;ill not appear");}
I hope this helps!
Alex Watts
8,396 PointsOK this is starting to make a little more sense, however why couldn't a and b just be added up in the function like so:
function addNumbers() { var a = 5; var b = 10; var total = a + b; }
Then that value could be used for a variety of different things.
Karolin Rafalski
11,368 PointsIn your case the variables are local (be sure to keep going in the videos if you haven't gotten to local and global variables- it will help!), they are inside the function, so another part of your code can't pass new values into your function.
try this in repl.it:
var a =parseInt(prompt("give me a number for the value of a"));
var b =parseInt(prompt("give me a number for the value of b"));
function addNumbers(a,b){
return a+b;}
console.log("The total is " + addNumbers(a,b));
In this code you can keep choosing new values for a and b and running the function over and over again. In your code, you would have to go back and change the values for a and b inside the function, inside your code, each time you wanted new numbers.