Skip to content

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:

Why is this bad?

Using bitwise operations to truncate numbers is not clear and do not work in some cases.

Examples

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

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-math-trunc": "error"
  }
}
bash
oxlint --deny unicorn/prefer-math-trunc

References