How to Check If a Number is Odd or Even in Javascript?

In this article, we are going to check if a number is odd or even. We have many methods to do this task but we use one of the easiest methods.


<!DOCTYPE html>
      <title>Check If a Number is Odd or Even</title>
         <div class="mb-3">
            <label for="number" class="form-label">Enter Number</label>
            <input type="text" class="form-control" id="number">
         <h4>The Entered Number is: <span id="result"></span></h4>
      <input type="button" value="Check" onclick="myFunction()" />


Here is using the modulus % operator to determine if a number is odd or even in JavaScript.

function myFunction() {
   var num = document.getElementById("number").value;
    alert("Please only enter numeric characters (Allowed input:0-9)")
  		if(num % 2 == 0) {
        var result = "Even!";
     } else {
        var result = "Odd!";
      document.getElementById("result").innerHTML = result;

I make numeric character validation so users can enter an only valid numeric values.