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
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 mynum // number typeof mynumStr // string
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
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
Number.MAX_SAFE_INTEGER
Number.MIN_SAFE_INTEGER
Edit: sry wanted to post on top level, somehow ended up as comment on yours
Awesome, thanks Jan!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Number.isInteger
method was designed to be used with numbers in order to test if it is an integer or not (NaN, Infinity, float...).developer.mozilla.org/en-US/docs/W...
Using
typeof mynum === "number"
is a better solution to test if a value is a number.Note, that the Number rounding of floats in JavaScript is not accurate
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
andNumber.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
Awesome, thanks Jan!