Check Even or Odd in JavaScript
n % 2 === 0. Remainder 0 means even. Use ===, not ==.
BeginnerConditional ProgramsExample 1 of 5
check-even-or-odd.js
1// Method 1: Using if-else2let number = 7;34if (number % 2 === 0) {5 console.log(number + " is even");6} else {7 console.log(number + " is odd");8}910// Method 2: Using ternary operator11let num = 8;12let result = (num % 2 === 0) ? "even" : "odd";13console.log(num + " is " + result);1415// Method 3: Using function16function checkEvenOdd(n) {17 if (n % 2 === 0) {18 return "even";19 } else {20 return "odd";21 }22}2324console.log("10 is " + checkEvenOdd(10));25console.log("15 is " + checkEvenOdd(15));
Output
7 is odd 8 is even 10 is even 15 is odd
What's going on
7 % 2 is 1 → odd. 8 % 2 is 0 → even.
-3 % 2 is -1 in JS, not 1. Python people trip here. Check n % 2 === 0 for even and treat the rest as odd.