Skip to content

Instantly share code, notes, and snippets.

@mikepenzin
Last active February 28, 2017 19:02
Show Gist options
  • Save mikepenzin/d5cf5060dfe4f95dc5af69945406e392 to your computer and use it in GitHub Desktop.
Save mikepenzin/d5cf5060dfe4f95dc5af69945406e392 to your computer and use it in GitHub Desktop.
Function isInteger(x) that determines if x is an integer.
// isInteger(3); // Outputs true
// isInteger("5"); // Outputs false
// isInteger("car"); // Outputs false
function isInteger(x) {
// ^ stands for XOR so every integer with (4^0) will give integer itself. Every string will give 0
return (x^0) === x;
}
// Another solution
function isInteger(x){
return Math.round(x) == x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment