Share this page 

Detect even/odd numberTag(s): String/Number


Use the modulus operator.

For Other interesting modulus operations, see http://mindprod.com/jgloss/modulus.html

if (x % 2 == 0) {
  // even 
}

if (x % 2 != 0) {
  // odd
}

... or binary AND operator...

if (( x & 1 ) == 0) {
  // even
}

if (( x & 1 ) != 0) {
  // odd
}