1

May I ask why the following code returns an error?

Error:

numberOne>numberTwo ? return true : false;
                      ^^^^^^
  SyntaxError: Unexpected token return
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)``

I have done everything I can think of, and I'm trying to use this format of if/else instead of the normal if () {} so that I can master all aspects of the language.

Code:

function isGreaterThan(numberOne, numberTwo) {
    numberOne>numberTwo ? return true : return false;
}

Thank you for any help you can provide!

Community
  • 1
  • 1
Death Waltz
  • 1,593
  • 1
  • 8
  • 24
  • 1
    The current answer has what you should use instead, but here’s more explanation: the conditional operator `… ? … : …` is (as the name suggests) an operator, so it operates on expressions and produces a value. A lot of expressions in JavaScript can have side-effects, like `i++` or `alert()`, but for the most part you’ll want to use the result of `?:`, as in `let higherNumber = a > b ? a : b;`. `return`, on the other hand, isn’t an expression at all – it’s its own statement. It doesn’t have a value, so you can’t use it in contexts like `?:` where an expression is expected. – Ry- Apr 04 '18 at 20:05
  • Thank you @nicael for correcting my errors! – Death Waltz Apr 04 '18 at 20:32

3 Answers3

5

You're misunderstanding the ternary operator. And in fact you don't have to use it there at all, just

return numberOne>numberTwo;

If you are still wondering what's wrong with your ternary operator, here's how you would get it with ternary. It would do here the same trick but in more characters (i.e., no need to use it here but here's an example anyway)

return (numberOne > numberTwo ? true : false);

In case I have been unclear - BOTH of those return statements will do EXACTLY the same thing.


Expanding a bit on the topic - why doesn't it work the way I'm using it?

The ternary operator is supposed, itself, to return the result (not to be confused with the return of the function). It returns the result by evaluating either the first expression (after ? and before :) or the second expression, which goes after :. In order for the ternary operator to return anything, both expressions must evaluate to anything. It doesn't make sense to put the return statement as one of the expressions inside the ternary operator as the return keyword doesn't evaluate to anything. I.e., writing the following is meaningless:

var lalala = return someVar;

and this is what the error is trying to tell you, it's the error of the same origin. I.e., it expected not return, but maybe someVar + anotherVar, 1 * 2, great > small or anything else which can be evaluated.

Recommended reading: How do you use the ? : (conditional) operator in JavaScript?

nicael
  • 16,503
  • 12
  • 50
  • 80
  • I am not attempting to return the value, i am trying to return if it is or is not greater with a Boolean value – Death Waltz Apr 04 '18 at 20:02
  • 1
    @Death That's **exactly** what my code does. `numberOne>numberTwo` returns the BOOLEAN. – nicael Apr 04 '18 at 20:03
  • 1
    This solves the problem but doesn't address the question of why it is an error to use `return` in a ternary expression. The answer being that `return` is a statement not an expression (it doesn't evaluate to anything, EG. `console.log( return 'foo' );` is meaningless because `return 'foo'` doesn't evaluate to anything you could log), so it can't be used where an expression is required. – Paul Apr 04 '18 at 20:06
  • @Paulpro that's a fair note though, I'll add that – nicael Apr 04 '18 at 20:06
1

What you are looking for is a plain old if statement:

if(numberOne > numberTwo) {
  return true;
} else {
  return false;
}

Cause an if statement branches statements. And return is a statement. A ternary however branches expressions, true and false are expressions, return isn’t. Therefore just move return in front of the ternary:

return numberOne > numberTwo ? true : false;

And that’s basically:

return numberOne > numberTwo;
Ry-
  • 199,309
  • 51
  • 404
  • 420
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
0

So “?:” operator returns something it self so you shouldn’t write return your self do it like this - function isGreaterThan(numberOne, numberTwo) {

return numberOne > numberTwo ? true : false;

}

AquaDev
  • 62
  • 7