3

Easy question. Does exist something like php's ?? in javascript?

In php you can do condition ?? statement2

which is the same as condition? condition : statement2.

Is there something like that in javascript?

Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38

2 Answers2

5

I would use the logical or operator (||)

console.log("true value" || "other")
console.log(false || "other")

This works because (source):

expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2

Francesco
  • 4,052
  • 2
  • 21
  • 29
0

So, if condition is false, go with statement2.

That's as simple as condition || statement2

If condition is truthy, if will be selected. If not, statement2 will be evaluated.

Devin Fields
  • 2,066
  • 1
  • 10
  • 18