What does Number.isInteger() check, and how is it different from typeof?
Short answer
Number.isInteger(x) returns true only if x is a number with no fractional part (e.g. 5, but not 5.5 or '5'); typeof would just say 'number' for both integers and decimals, without distinguishing them.
Advertisement
AModel answer
Number.isInteger(x) returns true only if x is a number with no fractional part (e.g. 5, but not 5.5 or '5'); typeof would just say 'number' for both integers and decimals, without distinguishing them.
Number.isInteger(5); // true
Number.isInteger(5.5); // false
Number.isInteger('5'); // false (not even a number)Real-world example
Validating that a quantity field entered by a user is a whole number, not a fraction.
Likely follow-ups
- What's the difference between Number.isInteger() and the global isFinite()?
Related topics