2

The scenario is the following: I want to return the value of a when a is defined and else I want to return b. I need an expression because of JSX.

I am looking for a better way to write the expression a ? a : b where a and b are variables. I was trying !a&&b. The problem is when a is defined, then it returns of course false instead of the value of a which I want to receive then. I think I have my wires crossed at the moment. If someone has an idea, I would highly appreciated it.

user
  • 117
  • 9

1 Answers1

2

You could take a default approach with a logical OR ||. This returns a, if this values is truthy, otherwise the default value of b.

a || b
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392