• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Check if a Number is Even or Odd in JavaScript

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

February 15, 2022 Leave a Comment

To tell if a Number in JavaScript is even or odd, we can use the modulus (remainder) operator % along with an if conditional statement.

if ((num % 2) == 0){
  //Number is Even
} else {
  //Number is Odd
}

If the remainder of a number after dividing by 2 is 0, then the number is even. If not, the number is odd.

We can also check if a number is odd using the operator %. If the remainder of a number after dividing by 2 is 1, then the number is odd.

if ((num % 2) == 0){
  //Number is Even
}
if ((num % 2) == 1){
  //Number is Odd
}

Tell if a Number in JavaScript is Even or Odd with a Click

Below we will provide code to let the user input a number, and then use the % operator to tell if the number is even or odd. Here is our simple HTML setup:

<p>Type a whole number to know if it's even or odd:</p>
<input id="userVal" type="text" value="">
<div id="click-me" onclick="evenOrOdd()">Get results</div>
<div id="results"></div>

First, we will add an onclick event to the submit button to run a function we will create.

We will then use the value property along with the getElementById method to get the value of the input.

We will then determine if the number is even or odd using the code we have above.

Finally, we will display the results using the textContent property.

function evenOrOdd(){
  
  //Get the user input
  var userInput = Number(document.getElementById("userVal").value);

  //Check if the number is even or odd and display results
  if ((userInput % 2) == 0){
    document.getElementById("results").textContent = "The Number is Even";
  } else if ((userInput % 2) == 1){
    document.getElementById("results").textContent = "The Number is Odd";
  } else {
    document.getElementById("results").textContent = "NaN";
  }
}

The final code and output for this example are below:

Code Output:

Type a whole number to know if it’s even or odd:

Get results

Full Code:

<p>Type a whole number to know if it's even or odd:</p>
<input id="userVal" type="text" value="">
<div id="click-me" onclick="evenOrOdd()">Get results</div>
<div id="results"></div>

<script>

function evenOrOdd(){
  var userInput = Number(document.getElementById("userVal").value);
  if ((userInput % 2) == 0){
    document.getElementById("results").textContent = "The Number is Even";
  } else if ((userInput % 2) == 1){
    document.getElementById("results").textContent = "The Number is Odd";
  } else {
    document.getElementById("results").textContent = "NaN";
  }
}

</script>

Hopefully this article has been useful in helping you tell if a number in JavaScript is even or odd.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Hide Element by Class
  • 2.  How to Select All Checkboxes in JavaScript
  • 3.  Remove Braces from String Using JavaScript
  • 4.  innerHTML vs outerHTML
  • 5.  Using JavaScript to Generate a Random Float Between 0 and 1
  • 6.  Using JavaScript to Remove Null Values From an Array
  • 7.  Using JavaScript to Get the Decimal Part of a Number
  • 8.  JavaScript atanh – Find Hyperbolic Arctangent of Number
  • 9.  Using JavaScript to Format the Date in mm dd yyyy
  • 10.  Using JavaScript to get the Last Element in Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy