A number is even if it divides exactly by 2 (no remainder), and odd otherwise. JavaScript gives you the remainder with the modulus operator %, so checking even or odd comes down to a single comparison: number % 2 === 0.
Program to Check Even or Odd Number in JavaScript
function checkEvenOdd(num) {
// Modulus operator gives the remainder after division by 2
if (num % 2 === 0) {
return `${num} is EVEN`;
} else {
return `${num} is ODD`;
}
}
console.log(checkEvenOdd(32));
console.log(checkEvenOdd(11));
Output:
32 is EVEN
11 is ODD
Negative numbers work correctly too — in JavaScript, -7 % 2 evaluates to -1, not 0, so the === 0 check still classifies -7 as odd rather than even.
Using the Ternary Operator
The if...else above can be compressed into a single line with the ternary operator (condition ? valueIfTrue : valueIfFalse), which is a common pattern once you're past the basics.
const num = 52;
const result = (num % 2 === 0) ? "EVEN" : "ODD";
console.log(`${num} is ${result}`);
Output:
52 is EVEN
