I want to understand how to use the return keyword with function properly and when to use it.
For instance, I tried the following code
function getWaterBottle(money) {
console.log(`You Can Buy "${parseInt(money / 1.5)}" Bottles Of Water`)
return money % 1.5;
}
getWaterBottle(4)
Then I thought to omit the return and add the same output to the console without the return keyword.
function getWaterBottle(money) {
console.log(`You Can Buy "${parseInt(money / 1.5)}" Bottles Of Water`)
console.log(`The change is ${money % 1.5}`) // this is the replacement of the return keybord
}
getWaterBottle(4)
So, my question is, what is the difference when using the return keyword and without it, and how and when to use it properly?
I appreciate your help.
3
Answers
here is the different between your two example
with
return
:without
return
:without
return
in second example, thedataFromFunction
isundefined
. thereturn
keyword will return whatever you write right after the keyword to the caller. find out more hereThe
return
keyword is not just a keyword but one of the most basic and fundamental concepts of computer programming.I want to store a value of the results of a calculation. I do that calculation with a
function
and store the result returned.For a pure function the important part is that the function does nothing on its own. It should take some values as arguments, perform a calculation, then return the results with no side effects.
In other words, calling a pure function without doing anything with the return value should be the same as not calling the function at all.
The log statements don’t change the state of your program so those are still pure functions. They just don’t do anything except read data and display it in the console.
Which in the end is the same as not being called when it comes to the actual state of your program.
The
return
in Javascript is called a statement. Its role is to return a value to the calling function. Some functions may not return a value, so thereturn
statement is not mandatory, it depends on what you are looking to do. No code after areturn
statement is executed. It is possible to use multiplereturn
in a function when we use multipleif
statement for example.You may use the
return
statement in a particular situation, since variables declared inside yourgetWaterBottle()
have a restricted scope to the body of the function, they are not visible from outside this function. To share the value of a variable declared inside your function to the calling function you use thereturn
statement.The value returned by the
return
statement can be assigned to a variable.For example, let use your code, in the following code you are calculating the change, but you are not using it. You are just displaying in the console the number of bottles you can buy with the money you have. The change is actually returned, but not used. We can use it in a variable for example:
This example is to make you understand how we can share the result of a function with the
return
statementIn another hand, you can use its value without assigning it to a variable, for example, we want to apply a discount to customers buying more than three bottles:
Now, to show you the use of multiple
return
statement, lets take the previous discount example:Again, these are examples, according to your project, the code can be made shorter with some good practice.
About your second code, not using the
return
statement means that the code inside your function is executed, but no value is returned, in fact it returnsundefined
and even if you declare variables inside it, you can not use them outside your function.