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?
Asked
Active
Viewed 244 times
-3

Om3ga
- 30,465
- 43
- 141
- 221
-
|| is Boolean logic 'OR'. – JAL Feb 27 '14 at 18:17
-
Short-circuit Evaluation: http://en.wikipedia.org/wiki/Short-circuit_evaluation – malifa Feb 27 '14 at 18:18
-
Why does everyone seem to be discovering how Javascript's logical operators work this week? Seen this question a few times recently. – deceze Feb 27 '14 at 18:18
-
@deceze Comes up multiple times daily. :) – epascarello Feb 27 '14 at 18:19
-
Get a good reference and look things up. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators – epascarello Feb 27 '14 at 18:20
3 Answers
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
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