What does console.log(4.9 | 0, -4.9 | 0, '7' | 0) output?
A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-169.js
1console.log(4.9|0,-4.9|0,'7'|0)
Coercion
Answer & explanation
Console output
4 -4 7
Why
Bitwise OR runs ToInt32 on each operand, which truncates toward zero (not floor). So 4.9 | 0 is 4 and -4.9 | 0 is -4, dropping the fraction in both directions toward zero. ToInt32 also coerces strings, so "7" | 0 becomes 7. The "| 0" idiom is a compact way to integer-truncate a value.