Basic JavaScript return statement

The code below is an example code from the basic JavaScript tutorial
“Introducing Else statements”

if (num > 10) {
  return "Bigger than 10";
} else {
  return "10 or Less";
}

I do not understand whether the use of return statement here is correct.
I am starting to get confused on whether I need to include a return statement
even in an if statement or not, because I used to think that they are used only for functions…

I basically don’t understand when to use return statements.

Can you please give us more context? What is the link to the challenge? What is the context around the code snippit you provided?

I am sorry,
it is from The Basic JavaScript Tutorial “Introducing Else Statement”

Hold on. I need to go look for the challenge so I can understand what is going on.

Edit: This challenge?

1 Like

It’s perfectly fine, assuming that this if/else is part of a funciton body and you want to exit the function at this point.

return statements are only used in functions, yes. They can be used in an if statement block.

The return statement is used to “exit” (or end) the function. Once a return statement is hit, no more code inside the function will run. If your function is supposed to return a value then it would be used in the way you have here (returning a string value). If you want to stop executing the function but don’t need to return a value you can use the command

return; //this returns undefined

If a function does not have a return statement, it will run until the last line of the function and then return undefined.

1 Like

The example is supposed to be from inside a function. You don’t need a return statement inside of every if statement. Return statements are used to pass information outside of a function. (Edit, and to stop the execution of the function, like @ArielLeslie said)

var x = 5;
if (3 > x) {
  console.log("That's impossible");
} else {
  console.log("I knew this would happen");
}

In this code, there is no need to return inside of the if.

function addTwo(a, b) {
  return a + b;
}

console.log(addTwo(1, 1)); // Hopefully logs 2

In this code, we need a return to get the sum of the two numbers out of the function scope.

Thank you very much for the explanation.
It was very clear and I was able to understand the concept!

1 Like

This return result is going to void because this statement returns a string but it doesn’t assign it to a variable or a constant. Therefore, you cannot reach out this return’s result.

I believe the example code snippit is intended to be from inside of a function, btw.

1 Like

There is absolutely nothing wrong with returning a string or any other value without assigning that value to a variable inside of a function. I don’t understand what you’re trying to say.

1 Like

I mean if you look at the given example about this problem by @kasaiy2002 doesn’t save the resulting value of return statement, so the value goes to void. i have referred to how to use return statement. I mean, while you use return statement, you must assign a variable this result of return statement.

and needless to say, but i have referred it for just if/else statement.

I really don’t understand what you are trying to say.

We don’t have any information about the calling context for this code snippit. It is incomplete code. We can add some context:

function checkNum(num) {
  if (num > 10) {
    return "Bigger than 10";
  } else {
    return "10 or Less";
  }
}

console.log(checkNum(5));

This is completely valid JS and that return statement is doing something.