console.log(0.5 | 0); // 0
console.log(-1 | 0); // -1
console.log(1 | 0); // 1
Why does 0.5 | 0
return zero, but any integer (including negative) returns the input integer? What does the single pipe ("|") do?
ベストアンサー1
This is a bitwise or.
Since bitwise operations only make sense on integers, 0.5
is truncated.
x | 0
is x
, if x
is an integer.