3

QUESTION

Is there a way to execute a return statement inside of a ternary operator?

It's a weird thing, because I feel like a ternary is like a shorthand for an if/else, and you CAN execute a return statement inside an if/else.

But for some reason, I'm not able to execute a return statement inside of a ternary operator:

Regular IF/ELSE:

// THIS WORKS
function foo() {
  if (true) {
    console.log('This is true');
  } else {
    console.log('Not true');
  }
}

TERNARY as IF/ELSE:

// THIS ALSO WORKS
function bar() {
  true ? 
    console.log('This is true')
  : console.log('Not true');
}

Both codes above works just fine. But when return is involved, the following happens:

Regular IF/ELSE with return:

// THIS WORKS
function foo() {
  if (true) {
    console.log('This is true');
  } else {
    return;
  }
}

TERNARY as IF/ELSE with return:

// THIS DOESN'T WORK
function bar() {
  true ? 
    console.log('This is true')
  : return;
}

CONCERNING POSSIBLE DUPLICATES:

I have looked around and didn't find an answer to this exact question.

It's clear to me that I can return a ternary operator, like the following piece of code, but this is not what this question is about.

function foo() {
  return(
    true ?
      console.log('This is true')
    : null
  );
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
  • 1
    The right sides of the `?` must evaluate to expressions, but `return` cannot evaluate to an expression; it must be a standalone statement. If you want to `return`, it needs to be outside. (similarly, `throw` cannot evaluate to an expression, though you can enclose a `throw` in an IIFE to throw, which you can't do with `return`) Going back to `if`/`else` is your only option – CertainPerformance May 10 '19 at 10:48
  • 3
    "*It's a weird thing, because I feel like a ternary is like a shorthand for an if/else*" well, it *isn't* and you really shouldn't think of it like this. It's a gross oversimplification that only confuses you. A ternary operator always produces one of two values - that's it. Or even more formally it operates on *three* expressions (hence *tern*ary - a specific form of an n-ary expression). One expression you evaluate, you execute one of the other two based on result. An `if` statement just runs one of two branches of code. They *might* do the same in some situations but aren't equivalent. – VLAZ May 10 '19 at 10:50
  • 1
    As an aside, if you're ever using a ternary just to save writing an `if`, then you're probably misusing the ternary. – VLAZ May 10 '19 at 10:51
  • 3
    The situation is almost exactly the same - you're trying to use a statement where an expression is required. The logic behind why it's not permitted is identical. – CertainPerformance May 10 '19 at 10:54
  • @CertainPerformance I guess you're technically right. But take a look in this article by Jeff Atwood (SO founder). Nevertheless, thanks for your help. [Dr. Strangedupe: Or, How I Learned to Stop Worrying And Love Duplication](https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/) – cbdeveloper May 10 '19 at 10:58
  • 2
    Why was the question reopened? Sure, maybe the dupe wasn't perfect but [that doesn't mean the question wasn't asked or answered before](https://stackoverflow.com/questions/35231609/why-we-cannot-have-return-in-ternary-operator). [More than once](https://stackoverflow.com/questions/19439219/ternary-operator-with-return-statements-javascript). [Or twice](https://stackoverflow.com/questions/39895655/using-return-as-one-of-multiple-statements-in-ternary-expression). Do we really need that many open questions for this? – VLAZ May 10 '19 at 11:06

2 Answers2

5

No its not possible. The operands of the ternary operator are expressions. And return is a statement. You should use if else. According to MDN

Parameters

condition
An expression whose value is used as a condition.

exprT, exprF
Expressions with values of any type.

Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

It's not possible - the ternary operator expects expressions, and return is a statement. Use a simple if statement:

function foo() {
    if (true) console.log("This is true"):
    else return;
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79