unicorn/prefer-number-properties

Require Number.isNaN, Number.parseInt and the like instead of the globals

fixable

The global isNaN and isFinite convert their argument to a number first, so isNaN("abc") is true and isFinite("1") is true. Number.isNaN and Number.isFinite don't convert, so they're false for anything that isn't a number.

Number.parseInt, Number.parseFloat and Number.NaN are the same as the globals. The rule reports the globals so that everything about numbers is written the same way.

--fix rewrites parseInt, parseFloat, NaN and Infinity. It doesn't touch isNaN and isFinite, because the result can change. --fix-dangerously rewrites those too.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/prefer-number-properties" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/prefer-number-properties" = ["error", {
  checkInfinity = false,
  checkNaN = true,
}]

Examples#

example.js4 problems
const width = parseInt(style.width, 10);

if (isNaN(width)) {
  reset(NaN);
}

const isValid = window.isFinite(width);
example.jsNo problems
const width = Number.parseInt(style.width, 10);

if (Number.isNaN(width)) {
  reset(Number.NaN);
}

const isValid = Number.isFinite(width);

Options#

OptionDefaultWhat it does
checkInfinityfalseAlso report Infinity and -Infinity. Write Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY.
checkNaNtrueReport NaN.

Infinity is only reported with checkInfinity:

example.jsNo problems
let lowest = Infinity;
example.jswith { "checkInfinity": true }1 problem
let lowest = Infinity;

With checkNaN: false, NaN is allowed:

example.jswith { "checkNaN": false }No problems
const missing = NaN;