Created
January 21, 2013 13:00
-
-
Save alanshaw/4585876 to your computer and use it in GitHub Desktop.
JavaScript test for integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isInt(value){ | |
return (Object.prototype.toString.call(value) == '[object Number]' && parseFloat(value) == parseInt(value)) && !isNaN(value); | |
} | |
/* | |
Test data: | |
isInt(null) -> false | |
isInt(undefined) -> false | |
isInt('') -> false | |
isInt('138') -> false | |
isInt('11.38') -> false | |
isInt({}) -> false | |
isInt({foo: 'bar'}) -> false | |
isInt([]) -> false | |
isInt([1, 2]) -> false | |
isInt([0]) -> false | |
isInt([1]) -> false | |
isInt(11.38) -> false | |
isInt(new Number(1.138)) -> false | |
isInt(new Number('blah') -> false | |
isInt(NaN) -> false | |
isInt(0) -> true | |
isInt(-138) -> true | |
isInt(1.0) -> true | |
isInt(new Number('138.0')) -> true | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment