Skip to content

Files

Latest commit

 

History

History
19 lines (13 loc) · 455 Bytes

prefer-math-trunc.md

File metadata and controls

19 lines (13 loc) · 455 Bytes

Pattern: Bitwise operation for number truncation

Issue: -

Description

Using Math.trunc() provides a clearer and more reliable way to truncate numbers than bitwise operations (x | 0, ~~x, x >> 0, x << 0, x ^ 0). Bitwise operations can be unclear and fail in certain edge cases.

Examples

Example of incorrect code:

const foo = 1.1 | 0;

Example of correct code:

const foo = Math.trunc(1.1);