DEV Community

Discussion on: My Favorite JavaScript Tips and Tricks

Collapse
 
hnicolas profile image
Nicolas Hervé

Number.isInteger method was designed to be used with numbers in order to test if it is an integer or not (NaN, Infinity, float...).

Number.isInteger(12) // true
Number.isInteger(12.0) // true
Number.isInteger(12.5) // false

developer.mozilla.org/en-US/docs/W...
Using typeof mynum === "number" is a better solution to test if a value is a number.

typeof mynum // number
typeof mynumStr // string
Collapse
 
jankapunkt profile image
Jan Küster • Edited

Note, that the Number rounding of floats in JavaScript is not accurate

(0.1 + 0.2) === 0.3 // false

Since this rounding affects integer representation, too, you should also consider Number.isSafeInteger

developer.mozilla.org/en-US/docs/W...

Also please consider Integer boundaries in JS, that are represented by Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER

developer.mozilla.org/en-US/docs/W...

developer.mozilla.org/en-US/docs/W...

Edit: sry wanted to post on top level, somehow ended up as comment on yours

Collapse
 
atapas profile image
Tapas Adhikary

Awesome, thanks Jan!