Linter · Rules
no-loss-of-precision
Disallow number literals that cannot be stored exactly as written
A JavaScript number holds 15 to 17 significant digits. A literal with more is rounded without a warning. 9007199254740993 becomes 9007199254740992, so an ID typed into the code points at another record. A literal that's too large becomes Infinity, and one that's too small becomes 0.
A number with a fractional part isn't reported by default. The extra digits of 3.14159265358979323846 only round it to the closest number there is, which is what you'd expect.
This rule is on by default.
Configure
On by default as an error. To turn it off:
[lint.rules]
"no-loss-of-precision" = "off" # or "warn"
Every option, set to its default:
[lint.rules]
"no-loss-of-precision" = ["error", {
ignoreDecimals = true,
}]
Examples
Reported:
const orderId = 9007199254740993;
const mask = 0x20000000000001;
const huge = 1e400;
const tiny = 1e-400;
Not reported:
const orderId = 9007199254740993n;
const orderKey = "9007199254740993";
const pi = 3.14159265358979323846;
const mask = 0x1fffffffffffff;
const huge = Infinity;
Options
| Option | Default | What it does |
|---|---|---|
ignoreDecimals | true | Don't report a number that has a fractional part. |
Only bun lint has this option. With an ESLint config file decimals are reported, as in ESLint.
const pi = 3.14159265358979323846;