math/prefer-number-is-integer
enforce the use of Number.isInteger() instead of other checking ways
- ⚙️ This rule is included in
"plugin:math/recommended"
. - 🔧 The
--fix
option on the command line can automatically fix some of the problems reported by this rule. - 💡 Some problems reported by this rule are manually fixable by editor suggestions.
📖 Rule Details
This rule aims to enforce the use of Number.isInteger()
instead of other checking ways to determine if a value is an integer.
Number.isInteger()
provides several advantages over manual integer checking:
- Type safety: Only returns
true
for actual numbers that are integers - No type coercion: Doesn't convert non-numbers to numbers before checking
- Clear semantics: Explicitly designed for integer checking
- Performance: Optimized native implementation
- Edge case handling: Properly handles special values like
Infinity
andNaN
Why Manual Checks Are Problematic
Manual integer checks using floor, ceil, trunc, or modulo operations can be:
- Less readable and express intent poorly
- Potentially less performant due to extra mathematical operations
- Prone to edge case issues with very large numbers or special values
- Not type-safe (may coerce non-numbers)
🔧 Options
Nothing.
📚 Further reading
- MDN - Number.isInteger()
- MDN - Math.trunc()
- MDN - Math.floor()
- MDN - Math.ceil()
- MDN - Math.round()
- MDN - Remainder (
%
) - MDN - parseInt()
🚀 Version
This rule was introduced in eslint-plugin-math v0.2.0