It’s a petty basic question which seems I don’t fully understand,
What exactly is the use of return? In what cases should I use it?
What would the difference between these code samples be?
function fun(text) {
console.log(text);
};
function fun(text) {
return console.log(text);
};
To be clear: I know what return does, I just don’t feel like I fully understand it’s purpose and when should I use it
You should use
returnif your function needs to, well, return anything to its caller. In your example, it’s not necessary to use return becauseconsole.logalready does what you want it to do. But, if your function calculates something (for example a mathematical operation like adding the first two params it receives), it’s logical it “returns” the result, like this:This way the function caller can use the result for whatever it’s doing.