Prefer Number static properties over global ones ​
💼 This rule is enabled in the ✅ recommended config.
🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
ECMAScript 2015 moved globals onto the Number constructor for consistency and to slightly improve them. This rule enforces their usage to limit the usage of globals:
Number.parseInt()overparseInt()(fixable)Number.parseFloat()overparseFloat()(fixable)Number.isNaN()overisNaN()(they have slightly different behavior)Number.isFinite()overisFinite()(they have slightly different behavior)Number.NaNoverNaN(fixable)Number.POSITIVE_INFINITYoverInfinity(fixable)Number.NEGATIVE_INFINITYover-Infinity(fixable)
Fail ​
js
const foo = parseInt('10', 2);js
const foo = parseFloat('10.5');js
const foo = isNaN(10);js
const foo = isFinite(10);js
if (Object.is(foo, NaN)) {}js
const {parseInt} = Number;
const foo = parseInt('10', 2);Pass ​
js
const foo = Number.parseInt('10', 2);js
const foo = Number.parseFloat('10.5');js
const foo = Number.isNaN(10);js
const foo = Number.isFinite(10);js
if (Object.is(foo, Number.NaN)) {}js
const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INFINITY;js
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;js
const isPositiveZero = value => value === 0 && 1 / value === Infinity;js
const isNegativeZero = value => value === 0 && 1 / value === -Infinity;Options ​
Type: object
checkInfinity ​
Type: boolean
Default: false
Pass checkInfinity: true to enable check on Infinity.
Fail ​
js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = Infinity;js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = -Infinity;Pass ​
js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = Number.POSITIVE_INFINITY;js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = Number.NEGATIVE_INFINITY;js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}]
const foo = Infinity;js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}]
const foo = -Infinity;js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INFINITY;js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;checkNaN ​
Type: boolean
Default: true
Pass checkNaN: false to disable check on NaN.
Fail ​
js
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = NaN;js
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = -NaN;Pass ​
js
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = Number.NaN;js
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = -Number.NaN;js
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}]
const foo = NaN;js
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}]
const foo = -NaN;