How to Check if a Number is Even or Odd in JavaScript? Solutions

Are you looking for ways to determine if a number is even or odd in JavaScript?

Don’t worry, we’ve got you covered! With just a few lines of code, we can show you how to easily check if a number is even or odd in JavaScript.

In this article, we’ll explore various methods for determining the parity of a number using JavaScript. So, if you want to improve your JavaScript skills, keep reading!

What is Even Number?

An even number is an integer that is divisible by 2 with no remainder.

In other words, when an even number is divided by 2, the result is an integer.

For instance, 4, 6, and 8 are all even numbers because they can be divided by 2 with no remainder.

What is Odd? Number

An odd number is an integer that is not divisible by 2. When an odd number is divided by 2, the result is not an integer.

For instance, 3, 5, and 7 are all odd numbers because they cannot be divided by 2 with no remainder.

In simple words, even numbers are divisible by 2 while odd numbers are not.

How to check if a number is even or odd in JavaScript?

You can check if a number is even or odd in JavaScript by using the modulo operator (%).

It returns the remainder when one number is divided by another. 

If a number is divisible by 2 with no remainder, it is even. Otherwise, it is odd.

Here’s an example of how you can use the modulo operator to check if a number is even or odd in JavaScript:

function isEvenOrOdd(num) {
  if (num % 2 === 0) {
    return 'even';
  } else {
    return 'odd';
  }
}

console.log(isEvenOrOdd(20)); 
console.log(isEvenOrOdd(15));

As you can see, in this example, the isEvenOrOdd function takes a number as an argument and returns ‘even’ if the number is divisible by 2 with no remainder, and ‘odd’ otherwise.

We can then use this function to check if a number is even or odd by passing it as an argument to the console.log function.

Output:

(20) even 

(15)
  odd

Different ways on how to check if a number is even or odd in JavaScript?

Aside from using the modulo operator (%), there are more useful different methods to check if a number is even or odd in JavaScript.

Here are some of the most common methods:

Using bitwise AND (&)

The bitwise AND operator compares each bit of two numbers and returns a new number where each bit is set to 1 if the corresponding bits of both numbers are 1, and 0 otherwise.

If the least significant bit (the rightmost bit) of a number is 1, the number is odd. Otherwise, it is even.

Here’s an example of how you can use the bitwise AND operator to check if a number is even or odd in JavaScript:

function isEvenOrOdd(num) {
  if (num & 1) {
    return 'odd';
  } else {
    return 'even';
  }
}

console.log(isEvenOrOdd(4)); 
console.log(isEvenOrOdd(5)); 

Output:

(4) even

(5) odd

Using a conditional (ternary) operator( ?: )

The conditional operator returns one of two values based on a condition.

You can use this operator in combination with the modulo operator to check if a number is even or odd in JavaScript.

Here’s an example:

function isEvenOrOdd(num) {
  return num % 2 === 0 ? 'even' : 'odd';
}

console.log(isEvenOrOdd(10)); 
console.log(isEvenOrOdd(11)); 

Output:

(10) even

(11)
 odd

Using the Math.abs() function

The Math.abs() function returns the absolute value of a number.

You can use this function in combination with the modulo operator to check if a number is even or odd in JavaScript, even if the number is negative.

Here’s an example:

function isEvenOrOdd(num) {
  return Math.abs(num) % 2 === 0 ? 'even' : 'odd';
}

console.log(isEvenOrOdd(-12)); 
console.log(isEvenOrOdd(-13)); 

Output:

(-12) even

(-13) odd

Conclusion

In conclusion, determining whether a number is even or odd in JavaScript is a straightforward task.

You can use various methods, including the modulo operator (%), bitwise AND operator (&), conditional (ternary) operator (?:), and the Math.abs() function, depending on your specific requirements.

Each method offers a reliable way to identify the parity of a number in JavaScript, helping you improve your coding skills and handle different scenarios efficiently.

We are hoping that this article provides you with enough information. That will help you understand the JavaScript even or odd.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Quick step-by-step summary (click to expand)
  1. What is Even Number. Read the ‘What is Even Number?’ section for the details and code.
  2. What is Odd? Number. Read the ‘What is Odd? Number’ section for the details and code.
  3. How to check if a number is even or odd in JavaScript. Read the ‘How to check if a number is even or odd in JavaScript?’ section for the details and code.
  4. Different ways on how to check if a number is even or odd in JavaScript. Read the ‘Different ways on how to check if a number is even or odd in JavaScript?’ section for the details and code.
  5. Conclusion. Read the ‘Conclusion’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment