/ Published in: JavaScript
URL: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Arithmetic_Operators
One of those common tools that's easy to forget about is the Modulus operator (%), which returns the remainder of a division operation.
If you divide some number by two, a remainder of 0 indicates an even number, while a remainder of 1 indicates an odd number.
Expand |
Embed | Plain Text
var isEven = function(someNumber){ return (someNumber%2 == 0) ? true : false; }; alert(isEven(64)); // Alerts "true". alert(isEven(97)); // Alerts "false".
Comments
Subscribe to comments
You need to login to post a comment.

Nice, just what I needed. Thanks!
I came across http://rindovincent.blogspot.com/p/javascript.html where there was a simple Javascript program to find whether the number is odd or even. I am pasting the same code with permission here.
var n = prompt("Enter a number to find odd or even", "Type your number here"); n = parseInt(n); if (isNaN(n)) { alert("Please Enter a Number"); } else if (n == 0) { alert("The number is zero"); } else if (n%2) { alert("The number is odd"); } else { alert("The number is even"); }