Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Created January 21, 2013 13:00
Show Gist options
  • Save alanshaw/4585876 to your computer and use it in GitHub Desktop.
Save alanshaw/4585876 to your computer and use it in GitHub Desktop.
JavaScript test for integer
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