Return to Snippet

Revision: 12756
at March 26, 2009 14:14 by jlizarraga


Initial Code
var isEven = function(someNumber){
    return (someNumber%2 == 0) ? true : false;
};

alert(isEven(64)); // Alerts "true".

alert(isEven(97)); // Alerts "false".

Initial URL
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Arithmetic_Operators

Initial Description
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.

Initial Title
Test for Even / Odd Numbers in Javascript

Initial Tags
javascript

Initial Language
JavaScript