-3

As I understand from Javascript || operator,
|| will return whichever operand is true, and
?: will return the 1st operand if the condition before ? evaluates to true, the 2nd otherwise.

But can || be considered as a ternary operator itself or not? Explain.

Community
  • 1
  • 1
Julien Lachal
  • 1,101
  • 14
  • 23

2 Answers2

4

The || operator can be seen as a special case of the ?: operator . The following snippets are identical:

var c = a || b;
var c = a ? a : b;
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • of course with a bit of terrible hackery by using accessors that change their own state they could be made to produce different results, but that's certainly an edge case. – zzzzBov Aug 05 '14 at 14:30
1

The || operator is binary because it has only two operands: condition1 || condition2.

?: is ternary because it has three operands: condition ? value_if_true : value_if_false.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tugdual
  • 81
  • 7