Skip to content

unicorn/prefer-math-trunc Pedantic ​

🚧 An auto-fix is still under development.

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:

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);

References ​

Released under the MIT License.