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
brandonlind2
7,823 Pointscan someone explain to me the point of a return value in a javascript function?
I'm watching javascript video on getting info from a function and I'm having trouble understanding why a return value is important.
I think I'll get the same result from these two pieces of code? What exactly does the return value add? Doesn't the first code display the same thing in the same way?
functions alertRandom () {var randomNumber= Math.floor(Math.random()*6+1);} ```
```javascript
functions alertRandom () {var randomNumber= Math.floor(Math.random()*6+1);
return randomNumber;} ```
3 Answers
Garry Hicks
1,345 PointsI can try and take a crack at this one - I would say that the difference is that with return you're assigning the ** return value** of that function - in the other case you could, depending on circumstance/usage, simply be declaring a function.
but this way you can just assign a variable to it:
function alertRandom () {
var randomNumber= Math.floor(Math.random()*6+1);
return randomNumber;
}
var sampleVariable = alertRandom();
works out much better as this though-
function alertRandom() {
return Math.floor(Math.random()*6+1);
}
more modular and reusable, and great for popping into say a complex function, or an Angular controller/service/factory, whatever your use case may be at the time. Good luck!
Quang Nguyen
2,235 PointsDear Lind,
From what I understand about the return statement in JS is that it stops the execution of a particular function and returns (shows) the value (result) from that function. This return statement, from my experience, mostly applies to many JS functions that involve in maths. For example:
function fix(fixNumber,decimalPlaces) {
var div = Math.pow(10,decimalPlaces);
fixNumber = Math.round(fixNumber * div) / div;
return fixNumber;
}
var number1 = prompt("Enter the number with decimal places that you want to fix","");
var number2 = prompt("How many decimal places do you want to fix for this number?","");
document.write(number1 + " fixed to " + number2 + " decimal places is: ");
document.write(fix(number1,number2));
I hope this will help you understand more about the return statement in JS.
Cheer, Quang (Farrell) Nguyen
Garry Hicks
1,345 PointsGood point Quang. Brandon - You can also use them in more than one place in the same function (as long as it actually solves a problem, rather than create a mess):
function findNeedleInHaystack() {
for (var hay in stack) {
if (hay === needle){
return 'needle';
}
}
return 'no luck';
}
a bit contrived, but you can see that at any point a return will exit the function with whatever value it's found,