-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
  • 30,465
  • 43
  • 141
  • 221

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
  • 198,278
  • 20
  • 158
  • 249
1

|| is a boolean or.

5 == true
0 == false

So, 5 || 0 = 5

Zero Fiber
  • 4,417
  • 2
  • 23
  • 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,811
  • 3
  • 38
  • 57