unicorn/prefer-math-trunc Pedantic
What it does
Prefers use of Math.trunc()
instead of bitwise operations for clarity and more reliable results.
It prevents the use of the following bitwise operations:
x | 0
(bitwise OR
with 0)~~x
(twobitwise NOT
)x >> 0
(Signed Right Shift
with 0)x << 0
(Left Shift
with 0)x ^ 0
(bitwise XOR Shift
with 0)
Why is this bad?
Using bitwise operations to truncate numbers is not clear and do not work in some cases.
Example
Examples of incorrect code for this rule:
javascript
const foo = 1.1 | 0;
Examples of correct code for this rule:
javascript
const foo = Math.trunc(1.1);