Use modulus:
// Will evaluate to true if the variable is divisible by 2
variable % 2 === 0  
    Seriously, there's no jQuery plugin for odd/even checks?
Well, not anymore - releasing "Oven" a jQuery plugin under the MIT license to test if a given number is Odd/Even.
Source code is also available at http://jsfiddle.net/7HQNG/
Test-suites are available at http://jsfiddle.net/zeuRV/
(function() {
    /*
     * isEven(n)
     * @args number n
     * @return boolean returns whether the given number is even
     */
    jQuery.isEven = function(number) {
        return number % 2 == 0;
    };
    /* isOdd(n)
     * @args number n
     * @return boolean returns whether the given number is odd
     */
    jQuery.isOdd = function(number) {
        return !jQuery.isEven(number);
    };
})();
    You don't need jQuery. Just use JavaScript's Modulo operator.
You can do it in a better way (up to 50 % faster than modulo operator):
odd: x & 1 even: !(x & 1)
Reference: High Performance JavaScript, 8. ->Bitwise Operators
You can use the modulus operator like this, no need for jQuery. Just replace the alerts with your code.
var x = 2;
if (x % 2 == 0)
{
  alert('even');
}
else
{
  alert('odd')
}
    You can also:
if (x & 1)
 itsOdd();
else
 itsEven();
    if (x & 1)
 itIsOddNumber();
else
 itIsEvenNumber();
    
                If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With