Do you know how to check if a number is even or odd in JavaScript in different ways? Maybe you are trying to build a game, a calculator, or just showing off your coding skills to others. Either way, we are enabling you to showcase your code expertise in NINJA level! π
In this article, weβll explore the “JavaScript Even or Odd” – easiest ways to check. Plus, we will add some twist to keep things fun. Letβs go! π
- The Quickest Way to Check Even or Odd (One-Liner!)
- The Classic Method (Using Modulo % Operator)
- A Fun Trick with Bitwise Operator &
- Even or Odd? Letβs Compare Methods!
- Best choice?
- Extra Fun: Check Even/Odd in One Line with Ternary Operator
- Why Should You Care About Even and Odd Numbers?
- Numeric and Binary Representation Table
- FAQs
- Conclusion
The Quickest Way to Check Even or Odd (One-Liner!)
If you are in a hurry, here is the One Liner JS code – the fastest way:
const isEven = num => (num & 1) === 0;
console.log(isEven(10)); // true
console.log(isEven(7)); // false

How does it work? π€
- The
& 1
operation checks the last binary digit of the number. - If itβs
0
, the number is even. - If itβs
1
, the number is odd.
The Classic Method (Using Modulo %
Operator)
This is the traditional way of checking if a number is even or odd:
function isEven(num) {
return num % 2 === 0;
}
console.log(isEven(10)); // true
console.log(isEven(7)); // false

How it Work?
- If a number divides evenly by
2
, itβs even. - If thereβs a remainder (
1
), itβs odd.
A Fun Trick with Bitwise Operator &
Letβs make things interesting with another bitwise trick:
const isOdd = num => num & 1;
console.log(isOdd(10)); // false
console.log(isOdd(7)); // true
How it Work?
- It works in just one line!
- This method is faster than
%
in some cases.
Even or Odd? Letβs Compare Methods!
Method | Code Simplicity | Speed | Readability |
---|---|---|---|
% Modulo Operator | β β β | β β | β β β β |
& Bitwise Operator | β β | β β β | β β |
One-Liner & 1 | β β β | β β β β | β β |

Best choice?
- Use
%
if you want readable code. - Use
&
if you love bitwise tricks. - Use
& 1
for one-liner magic.
Extra Fun: Check Even/Odd in One Line with Ternary Operator
Want to build a cool one-liner JS code that returns “Even” or “Odd”? Here are the code:
const checkEvenOdd = num => num % 2 === 0 ? "Even" : "Odd";
console.log(checkEvenOdd(10)); // "Even"
console.log(checkEvenOdd(7)); // "Odd"
Super clean, right? π
Why Should You Care About Even and Odd Numbers?
- Game Development: Checking scores, moves, or turns.
- Animations: Create effects based on even/odd positions.
- Sorting Algorithms: Even/odd division helps in bucket sorting.
- Fun Fact: Many magic tricks rely on even/odd logic! π©β¨
Numeric and Binary Representation Table
Number | Binary Representation | Even/Odd Check Method |
---|---|---|
0 | 0000 | Even (0 & 1 === 0 ) |
1 | 000 1 | Odd (1 & 1 === 1 ) |
2 | 0010 | Even (2 & 1 === 0 ) |
3 | 0011 | Odd (3 & 1 === 1 ) |
4 | 0100 | Even (4 & 1 === 0 ) |
5 | 0101 | Odd (5 & 1 === 1 ) |
6 | 0110 | Even (6 & 1 === 0 ) |
7 | 0111 | Odd (7 & 1 === 1 ) |
8 | 1000 | Even (8 & 1 === 0 ) |
9 | 1001 | Odd (9 & 1 === 1 ) |
10 | 1010 | Even (10 & 1 === 0 ) |
FAQs
1. What happens if I check a decimal number?
If you use % 2
on a decimal, it still works! Example:console.log(3.5 % 2); // 1.5 (not even!)
2. Can I check even/odd for negative numbers?
Yes! It works the same way:console.log(isEven(-4)); // true
console.log(isEven(-7)); // false
3. Which method should I use?
If youβre learning, use %
. If you want speed, try &
.
4. Can I use AI to check even or odd numbers?
Yes! AI-powered coding assistants like GitHub Copilot can generate JavaScript functions for checking even or odd numbers, making coding even easier!
5. Does JavaScript have a built-in method for checking even/odd?
No, JavaScript doesnβt have a built-in isEven()
or isOdd()
function, so you need to use %
or &
to check it yourself.
Conclusion
Checking if a number is even or odd is super easy in JavaScript! Whether you use modulo (%
), bitwise (& 1
), or a one-liner, you now have three cool ways to do it. π
π‘ Try our code examples on “JavaScript Even or Odd” and impress your coding buddies!
Which one you are trying? Let us know by writing a comment. Thanks !
ποΈββοΈ Discover Code Blocks From 20+ yrs JS Expert
π₯ Asp.net C# Developer
π Solution Architect
π¨ββοΈ Database Administrator
π’ Speaker
π MCTS since 2009
Leave a Reply