JavaScript: Check Even or Odd Numbers - W3Schools Tutorial

Sarah Jul 13, 2026

When working with numbers in JavaScript, determining whether a number is even or odd is a common task. In this article, we'll explore methods to check the evenness or oddness of a number, drawing insights from W3Schools and other reliable sources. Let's dive in.

odd or even numbers worksheet
odd or even numbers worksheet

Before we proceed, it's essential to understand that JavaScript treats numbers as floating-point values, which means they have a decimal point. This distinction will become crucial when we discuss methods to check for even or odd numbers.

an image of a computer screen with the text check if a number is even odd
an image of a computer screen with the text check if a number is even odd

Checking Even or Odd using Modulo Operator

The modulo operator (%) returns the remainder of a division operation. It's the most straightforward method to check if a number is even or odd in JavaScript.

even and odd number
even and odd number

When a number is divided by 2, if the remainder is 0, the number is even; otherwise, it's odd. Here's a simple function demonstrating this:

```javascript function isEven(n) { return n % 2 === 0; } function isOdd(n) { return n % 2 !== 0; } ```

Checking Even or Odd with Bitwise Operator

an image of a computer screen with text and numbers on it, including the number 1
an image of a computer screen with text and numbers on it, including the number 1

Advanced users might prefer using bitwise operators for checking evenness or oddness. A number is even if its least significant bit (rightmost bit) is 0, and it's odd if this bit is 1.

Here's how you can use the bitwise AND operator (& 1) to check even or odd:

```javascript function isEvenBitWise(n) { return (n & 1) === 0; } function isOddBitWise(n) { return (n & 1) !== 0; } ```

Discrepancies with Floating-Point Numbers

two different types of font and numbers on the same page, one is for an email
two different types of font and numbers on the same page, one is for an email

Recall that JavaScript numbers are floating-point. When you check the even or odd of such numbers, you might encounter unexpected results due to precision errors. For instance, sentences`isEven(0.1)` and `isEven(0.2)` might return `false` due to precision issues.

To handle such cases, you can compare the number with the absolute difference between it and an integer. If this difference is less than a small threshold (like 1e-10), treat the number as the integer value.

Checking Even or Odd using Math.floor() and Math.abs()

Even and Odd Numbers Song for Kids | Odds and Evens for Grades 2 & 3
Even and Odd Numbers Song for Kids | Odds and Evens for Grades 2 & 3

Another approach to check evenness or oddness is by using the `Math.floor()` function to convert the number to the nearest lower integer, then checking the absolute value of this integer against 0.

Here's how you can create functions using this method:

C Program to Check a number is Even or Odd
C Program to Check a number is Even or Odd
the cover of 5 count even and odd numbers in an array description, this program counts how many even and odd numbers are present in an array
the cover of 5 count even and odd numbers in an array description, this program counts how many even and odd numbers are present in an array
an image of a phone screen with the text,'how many numbers are there? '
an image of a phone screen with the text,'how many numbers are there? '
Whack a Dino(Game) | Made of HTML, CSS and JavaScript
Whack a Dino(Game) | Made of HTML, CSS and JavaScript
Odd and Even Numbers Worksheets With Answers
Odd and Even Numbers Worksheets With Answers
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
Add Commas To Numbers In Javascript
Add Commas To Numbers In Javascript
Sort array of numbers in JavaScript
Sort array of numbers in JavaScript
Even and Odd Numbers
Even and Odd Numbers
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
Best 40+ javascript projects for beginners With Source Code
Best 40+ javascript projects for beginners With Source Code
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨
Are You Getting Ready to Teach Even and Odd Numbers in Second Grade?
Are You Getting Ready to Teach Even and Odd Numbers in Second Grade?
an image of a black screen with text on it that says, add seed to your answer function what's the output?
an image of a black screen with text on it that says, add seed to your answer function what's the output?
Data Types in JavaScript
Data Types in JavaScript
Add Table Rows & Cells In Javascript
Add Table Rows & Cells In Javascript
Even and Odd Numbers for Kids | Elementary Math Lesson
Even and Odd Numbers for Kids | Elementary Math Lesson
a poster with the names and numbers of different programming languages in orange, black and white
a poster with the names and numbers of different programming languages in orange, black and white
an image of a computer screen with numbers and empties on it, including smiley faces
an image of a computer screen with numbers and empties on it, including smiley faces
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡

```javascript function isEvenMath(n) { return Math.abs(Math.floor(n)) === 0; } function isOddMath(n) { return Math.abs(Math.floor(n)) !== 0; } ```

Each of these methods has its advantages and trade-offs, and the best choice depends on your specific use case. Now that you've learned multiple ways to check whether a number is even or odd in JavaScript, you can make informed decisions in your coding projects. Happy coding!