-3

As the title says everything. 5 || 0 and 0 || 5 returns 5 in JavaScript. Why does this happen and what does two || means in javascript?

Om3ga
  • 26,989
  • 41
  • 135
  • 210

3 Answers3

1

It's a boolean or, and 5 evaluates to truthy. If you want to force your types to boolean you should use the !! (double negation) like so,

!!(5 || 0)
Elliott Frisch
  • 191,680
  • 20
  • 149
  • 239
1

|| is a boolean or.

5 == true
0 == false

So, 5 || 0 = 5

Zero Fiber
  • 4,239
  • 2
  • 21
  • 34
0

The || is a synonym for the logical OR

So the statement ANY_VALUE || ANY_OTHER_VALUE means that if the first value is truthy then return that else return the second value

Lucky Soni
  • 6,636
  • 3
  • 35
  • 56