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 JavaScript Basics (Retired) Creating Reusable Code with Functions Getting Information From a Function

Asad Sajon
PLUS
Asad Sajon
Courses Plus Student 918 Points

Why i need to use return value?

Hi there!i need a clear conception from you!why i need to use return value in Javascript function?Please see my works in below,i don't understand what's the differntiate between these?

work without return value:

<script type="text/javascript">
function getRandomNumber(){
var randomNum=Math.floor(Math.random()*6)+1;
alert(randomNum);
}
getRandomNumber();
</script>

work with return value:

<script type="text/javascript">
function getRandomNumber(){
var randomNum=Math.floor(Math.random()*6)+1;
return randomNum;
}
alert(getRandomNumber());
</script>

2 Answers

Petros Sordinas
Petros Sordinas
16,181 Points

You don't "need" to return a value from a function, it is a matter of best practices. Generally, you want your functions to be responsible for one thing. It makes it easy to read and debug your code.

With your example, the function without the return value does two things: It computes the random number and alerts the result. The function with the return value does one thing, basically what the function name says it will do - produce a random number.

With your first example you can only use it if you want to display the random number. With your second example, you can use the result of the function for anything you want; display it, use it in a calculation, save it somewhere else and so on.

There are cases when you don't have to return a value; you just want the function to do something. The alert function you are using is an example. However, it does one thing only, takes a string value as input and display it in an alert box.

Zubair Siddique
Zubair Siddique
4,889 Points

Thanks for your explanation. Just few more questions. You said that in example one, function does two things. Whereas in example two, it does one thing. I think in example two, it's also doing two things, returning and then alerting. Correct me if i'm wrong.

And, my second concern is, instead of return, can't we just use a function to use it anywhere we want, display, print or store ?

Thank you for the explanation. Could it be understood that return can produce something (a value) that is stored inside a function, and it could be used later?

Petros Sordinas
Petros Sordinas
16,181 Points

Hi Zubair,

In the second example, the function just returns the number. The alert is done after the function call, with the result of the function, so the function doesn't do both. The example as a complete script, does.

In regards to your second concern: Yes, you can. You can write a function that does all that and even more. By doing that however, your function is useful only if you want to do all these things. It is not so much a matter of if you can do something but how you want to do something.

For example, with random numbers. How many times do we expect to generate the random number during our app runtime? How many times do we expect to print/display or generally manipulate that number? If our app generates a random number 10 times and prints these numbers to the display, then our app is small and we could put the random number generation and printing in the same function, or even a loop. If our app generates a random number for different purposes and sometimes we display it, sometimes we use it to manipulate some other data, then it is best to have a function that only produces a random number and other functions for other tasks that call the random number function generator.

Hope this makes sense to you.