Return?


#1

I don't really understand the return keyword. Please help me clarify this as simple as possible. Thanks!


#2

return stops execution of a function and returns some value from that function

function myFunc() {
  return 'Hello world.';
}
var hi = myFunc();
console.log(hi);

This would output 'Hello world.'. Function will terminate after the return and no other code should be put there, but even if it is, it wont get executed. So just use return when you want your function to do something for your and then return some value that you want to use in future.


#3

what do you mean by returning some value to use in the future, can you give me an example ?


#4

Sure. I assigned var hi = myFunc();. That means that whatever is returned inside the function gets saved to variable hi. Here are 2 more examples.

function square(n) {
  return n * n;
}
function sentence(name) {
  return "Hello, my name is " + name + ".";
}
var squared = square(4); // returns 4 * 4  = 16 from square function
var hiAgain = sentence('Igor'); // returns "Hellp, my name is Igor." from sentence function

console.log(square(squared)); // return 16 * 16 = 256 from square, this is how you can reuse values that you return from functions

I hope this helps you to understand better what I wanted to say. If you need further help, let me know.


#5

so the formula of the return can be reuse over and over again right?


#6

I'm not sure what you mean by formula.
You can call it on a variable and store the returned value in one. You can call it with different (or even same) parameters and store it in 2nd variable, then 3rd, etc. Functions are made to be reused, so you're free to use it as many times as you like. And each time you want to save the returned value, you can assign it to a variable.


#7

thanks, that really helps


#8

wait, in the above example, what if I don't use the return keyword, like this:
function square(n) {
n * n;
};
so what's the different now ?


#9

Then you'd compute n*n but as in this case this value is never used again it's kind of pointless.


#10

What @haxor789 said. There is no point doing that if you never use it again. And it's actually good practice to have a return statement. Even when you want a function to do something for you, like upload a picture, but you don't want to save anything from it. You can still return true or false to inform yourself if function executed as expected.

So, to summarize. Try to have return in your functions. If you plan to use values calculated inside them, return those values. If you don't plan to reuse the values calculated inside them, try to return true if something that you expected occurred, and false if something unexpected happened (and yes, try to predict unexpected behavior, for example, if you expect a result to be less than 5, then write something like this:

function example() {
  // name, variables, calculations...
  if (result < 5) {
  // do something here if needed
  return true;
  // if you need calculated value, you can return that
  // return result
  } else {
    return false;
  }
}