This rule raises an issue when bitwise operators like | 0, ~~, >> 0, << 0, or ^ 0 are used to truncate numbers instead of using Math.trunc().

Why is this an issue?

Using bitwise operators to truncate numbers is a common JavaScript "hack" that appears in many codebases. While these operations can truncate decimal numbers to integers, they create several problems:

Clarity and Intent: Bitwise operations like x | 0 or ~~x are not immediately clear to most developers. The intent to truncate a number is hidden behind cryptic syntax that requires specific JavaScript knowledge to understand.

Reliability Issues: These bitwise operations only work correctly for numbers within the 32-bit signed integer range (-2,147,483,648 to 2,147,483,647). For numbers outside this range, the results can be unexpected and incorrect. For example, 9999999999 | 0 returns 1410065407 instead of 9999999999.

Maintenance Burden: Code using these patterns is harder to maintain because:

Modern Alternative: Math.trunc() was introduced in ES2015 specifically for this purpose. It clearly expresses the intent to truncate a number and works correctly for all JavaScript numbers, including those outside the 32-bit integer range.

What is the potential impact?

Using bitwise operators for number truncation can lead to incorrect results when working with large numbers, making applications unreliable. The unclear syntax also reduces code maintainability and increases the likelihood of bugs during development and maintenance.

How to fix?

Replace bitwise OR with zero with Math.trunc() to clearly express the intent and ensure correct behavior for all numbers.

Non-compliant code example

const result = value | 0; // Noncompliant

Compliant code example

const result = Math.trunc(value);

Documentation