Check if a number is even or odd in JavaScript
Beginner · 3 min read
Even or odd is one of the first checks every programmer writes. You use it to color alternate table rows, split players into two teams, or decide whose turn it is. It looks like one line of code, and the most common version of that line is wrong for negative numbers.
In this tutorial you will write isEven(n) and isOdd(n) that work for negatives, zero and decimals, and see where the two popular shortcuts go wrong. You need to know what a function is. The % operator is explained below.
Even, odd, and the remainder
A whole number is even if it divides by 2 with nothing left over, and odd if 1 is left over. Zero counts as even. The % operator gives what is left over after a division:
console.log(10 % 2); // 0: even
console.log(7 % 2); // 1: odd
console.log(0 % 2); // 0: even
So "is even" is "the remainder after dividing by 2 is 0".
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
59 Even or odd
easyDecide whether a number is even. Make it right for negatives and for numbers that are not whole, then compare it with the bitwise trick people reach for.
Stuck? Three hints, smallest first
- 1.
n % 2 === 0for even. Test it on-4before you trust it. - 2. For odd,
n % 2 === 1is wrong on negatives. Work out what-3 % 2is. - 3.
n & 1is fast but converts to a 32-bit integer first, so it has an opinion about2.5.
Step 1: isEven
const isEven = (n) => n % 2 === 0;
console.log(isEven(10)); // true
console.log(isEven(0)); // true
console.log(isEven(-4)); // true
console.log(isEven(7)); // false
n % 2 === 0 is already true or false, so the function can return it directly. It works for negative even numbers too, because -4 % 2 is 0.
Step 2: the tempting isOdd
Odd means "1 left over", so this looks right:
const oddNaive = (n) => n % 2 === 1;
console.log(oddNaive(7)); // true
console.log(oddNaive(-3)); // false
-3 is odd, yet it says false. Look at the remainder:
console.log(-3 % 2); // -1
JavaScript's % gives a remainder with the same sign as the number on the left. For a negative odd number, that is -1, not 1. This is the version of isOdd that ships in real code, because it reads exactly like the definition of odd.
Step 3: a safe isOdd
Math.abs removes the minus sign, so -1 becomes 1:
const isOdd = (n) => Math.abs(n % 2) === 1;
console.log(isOdd(-3)); // true
console.log(isOdd(7)); // true
console.log(isOdd(10)); // false
You could also write n % 2 !== 0. Step 4 shows why the answer doesn't: that version would call 2.5 odd.
Step 4: numbers that aren't whole
Is 2.5 even? No. It isn't odd either, because "even" and "odd" only apply to whole numbers. Both functions handle that:
const isEven = (n) => n % 2 === 0;
const isOdd = (n) => Math.abs(n % 2) === 1;
console.log(2.5 % 2); // 0.5
console.log(isEven(2.5)); // false
console.log(isOdd(2.5)); // false
The remainder is 0.5, which is neither 0 nor 1, so both say false.
Step 5: the bitwise shortcut
You may see (n & 1) === 0 used for "is even". & is a bitwise operator: it looks at the number as binary digits, and the last binary digit of a whole number is 1 when it is odd. To do that, & first turns the number into a 32-bit whole number, dropping any decimals:
const evenBitwise = (n) => (n & 1) === 0;
console.log(evenBitwise(10)); // true
console.log(evenBitwise(2.5)); // true
2.5 became 2, and 2 is even, so the shortcut says 2.5 is even. If the input could ever be a decimal, or bigger than 32 bits can hold, use %. If you need to be sure the input is whole, check Number.isInteger(n) first.
The full solution
The answer panel has isEven, isOdd and the two shortcuts, and prints even and odd for six sample numbers, including -4, -3, 0 and 2.5. The checks show the naive odd test failing on -3 and the bitwise test failing on 2.5. Press Run to see the output.
const isEven = (n) => n % 2 === 0;
const isOdd = (n) => Math.abs(n % 2) === 1; // abs, because of negatives
// The tempting shortcuts, and where each one goes wrong.
const oddNaive = (n) => n % 2 === 1; // wrong for negatives
const evenBitwise = (n) => (n & 1) === 0; // wrong for fractions
const sample = [-4, -3, 0, 2.5, 7, 10];
for (const n of sample) {
console.log(String(n).padStart(5), 'even', isEven(n), ' odd', isOdd(n));
}
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('a positive even', isEven(10), true);
test('zero counts as even', isEven(0), true);
test('a negative even', isEven(-4), true);
test('a negative odd', isOdd(-3), true);
test('but the naive odd test misses it', oddNaive(-3), false);
test('because -3 % 2 is -1, not 1', -3 % 2, -1);
test('2.5 is not an even number', isEven(2.5), false);
test('the bitwise version wrongly says it is', evenBitwise(2.5), true);
n % 2 === 1 is the bug that ships. It reads like the definition of odd and it is false for every negative odd number, because the remainder carries the dividend's sign — exercise 58's rule showing up in a two-line helper.
n & 1 looks clever and coerces to a 32-bit integer first, so 2.5 becomes 2 and is reported even. If the input might be fractional, or larger than 2³¹, use % and check Number.isInteger first.
Common mistakes
- Writing
n % 2 === 1for odd. It is false for every negative odd number, because-3 % 2is -1. - Trusting
n & 1for any number. It drops decimals first, so 2.5 counts as even. - Forgetting zero. Zero is even, and
isEven(0)should betrue.
Try it yourself
- Use
isEvenwithfilterto keep only the even numbers in[1, 2, 3, 4, 5, 6]. - Give the rows of a 6-row table alternating labels,
'light'and'dark', using the row's position. - Write
parity(n)that returns'even','odd'or'not a whole number', usingNumber.isInteger.