Check Even or Odd in JavaScript
Check if a number is even or odd in JavaScript using n % 2 === 0. Covers if-else, the ternary operator and negative numbers, with explanation and output.
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));
Sample Output
7 is odd 8 is even 10 is even 15 is odd
In this one
- How the % operator finds a remainder
- Why === is safer than ==
- How to write if-else and the ternary operator
- How negative numbers behave with %
How it works
- 1
number is 7. 7 % 2 is 1, so the else branch prints "7 is odd".
- 2
num is 8. The ternary returns "even".
- 3
checkEvenOdd(10) returns "even".
- 4
checkEvenOdd(15) returns "odd".
Edge cases
Negative odd numbers
-3 % 2 === 1 is false.
The remainder keeps the sign, so it is -1. Check for even with === 0 and call the rest odd.
Decimals
4.5 % 2 is 0.5, so the code says odd.
Odd and even only apply to whole numbers. Check Number.isInteger(n) first.
String input
"8" % 2 is 0 because % converts the string.
It works here, but convert input with Number() anyway so other operators behave.
What's going on
This program tells you whether a number is even or odd. An even number divides by 2 with nothing left over. An odd number leaves 1. Think of splitting students into pairs for a lab. If the class count is even, nobody is left alone.
The key tool is the remainder operator %. 7 % 2 is 1, so 7 is odd. 8 % 2 is 0, so 8 is even. The test is n % 2 === 0.
Use === and not ==. === checks value and type with no guessing. == tries to convert types first, which can hide bugs. For example "8" == 8 is true, but "8" === 8 is false. Strict checks make your intent clear.
The program shows three ways to write the same idea. Method 1 uses if-else. Method 2 uses the ternary operator, a short form of if-else written as condition ? valueIfTrue : valueIfFalse. Method 3 puts the check in a function so you can call it with any number.
Watch out for negative numbers. In JavaScript -3 % 2 is -1, not 1. People who know Python get surprised here. So do not test n % 2 === 1 for odd. Test n % 2 === 0 for even and treat everything else as odd. That way negatives work too.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is 9 % 2?
Pick an answer to check it.
Question 2
What does -5 % 2 return in JavaScript?
Pick an answer to check it.
Questions people ask
Why not use == here?
n % 2 is always a number, so == would work. Using === everywhere builds a safe habit.
Is 0 even?
Yes. 0 % 2 is 0, so the program says even.
Is there a faster way?
n & 1 also works for whole numbers, but % is clearer for beginners.