• 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
  • VBA
  • 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.  JavaScript indexOf – Get the position of a value within a string
  • 2.  How to Exit for Loop in JavaScript
  • 3.  JavaScript cosh – Find Hyperbolic Cosine of Number Using Math.cosh()
  • 4.  Using the appendChild Method with JavaScript to Add Items to a List
  • 5.  How to Return Multiple Values in JavaScript
  • 6.  Using JavaScript to Set the Width of a Div
  • 7.  Using JavaScript to Set Select to the First Option
  • 8.  JavaScript Infinite Loop
  • 9.  Remove Commas From a String in JavaScript
  • 10.  JavaScript acosh – Find Hyperbolic Arccosine of Number

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy