Skip to content

Latest commit

 

History

History
51 lines (36 loc) · 1.59 KB

prefer-number-is-integer.md

File metadata and controls

51 lines (36 loc) · 1.59 KB

Prefer Number.isInteger() for integer checking

This rule is part of the recommended config.

💡 This rule provides suggestions.

Enforces the use of Number.isInteger() for checking if a number is an integer.

There are multiple ways to check if a variable is an integer, but these approaches tend to have slightly different behaviours.

For example:

// this is not an integer (or a number)
let notInteger = [['1']];

notInteger % 1 === 0; // true - ?! an array is defintely not an integer
Number.isInteger(notInteger); // false - makes sense

// this is an integer that is larger than Number.MAX_SAFE_INTEGER
let largeInteger = 1_000_000_000_000_000_000; 

largeInteger^0 === largeInteger; // false - its an integer, should be true
Number.isInteger(largeInteger); // true - makes sense

Due to the difference in behaviours across the different implementations, this rule is fixable via the suggestions API.

Fail

(value^0) === value
(value | 0) === value
Math.round(value) === value
parseInt(value, 10) === value
~~value === value

// these will all trigger the lint warning
_.isInteger(value);
lodash.isInteger(value);
underscore.isInteger(value);

Pass

Number.isInteger(value);