Write a program using JavaScript check whether the input number is even or odd

In this tutorial, we are going to see how to determine if a number is odd or even in JavaScript. Given an integer and we need to check whether it is odd or even using Javascript.

An even number is an integer correctly divisible by 2. Example: 0, 4, 8, etc.

An odd number is an integer that is not correctly divisible by 2. Example: 1, 3, 7, 15, etc.

To check whether the given number is even or odd, we check the remainder of the division by dividing the number by 2.  

<!doctype html> <html> <head> <script> function check(){ var nbr; nbr = Number(document.getElementById("myInput").value); if(nbr%2 == 0) { alert("Even number"); } else { alert("Odd number"); } } </script> </head> <body> Enter a number : <input id="myInput"> <button onclick="check()">Check</button> </body> </html>

Write a program using JavaScript check whether the input number is even or odd

Tags: JavascriptMath()utility function Reading Time: 2 minutes 🕑

Last updated: March 24, 2022.

There is no in-built function or method to check if a number is odd or even in JavaScript. We therefore have to create a function ourselves to perform this check.

This can be done with the help of the remainder operator (%).

Since an even number always has a remainder of zero, if applying the remainder operator to a number returns this outcome, the number must be even. Conversely, if a remainder exists, the number must be odd.

Below is a function that perform this check, including a pre-check for whether the number passed into it when it is called is an integer. The function is then called three times:

function oddOrEven(x) { // First, check if number is integer. If not, // keyword return stops function processing // and a console error is created if (!Number.isInteger(x)) { return console.error("Not an integer!") } // Checks the remainder of the number passed // into the function if divided by 2. If it // is 0, the if statement is executed, if it // is not, the else statement runs. if (x % 2 === 0) { console.log("The number is even"); } else { console.log("The number is odd"); } } oddOrEven(2) // The number is even oddOrEven(3) // The number is odd oddOrEven(0.2) // Not an integer

Alternative solution using the ternary operator

The odd or even number check function can also be written using the ternary operator. The syntax is as follows: statement : returnIfTrue : returnIfFalse.

Note that because a ternary statement requires a returnIfFalse expression, it is only applied to the check of whether a number is even or odd:

function oddOrEven(x) { if (!Number.isInteger(x)) { return console.error("Not an integer") } x % 2 === 0 ? console.log("The number is even") : console.log("The number is odd") } oddOrEven(2) // The number is even oddOrEven(3) // The number is odd oddOrEven(0.2) // Not an integer

Write a JavaScript program to check whether a given number is even or odd number.



Source Code

<html> <head> <title>Even or Odd number</title> </head> <body> <script> var no = parseInt(prompt("Enter the number", "")); if(no%2 == 0) document.write("The number is Even."); else document.write("The number is Odd."); </script> </body> </html>

Write a program using JavaScript check whether the input number is even or odd

<!doctype html> <html> <head> <script> function odd_even(){ var no; no=Number(document.getElementById("no_input").value); if(no%2==0) { alert("Even Number"); } else { alert("Odd Number"); } } </script> </head> <body> Enter Any Number:<input id="no_input"><br /> <button onclick="odd_even()">Click me</button> </body> </html>

  • no=Number(document.getElementById("no_input").value);
    This code is used for receive input value form input field which have id no_input.
  • <button onclick="odd_even()">Click Me</button>
    This code is used for call odd_even() function when button clicked.

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: or 9999595223

Write a program using JavaScript check whether the input number is even or odd

Even numbers are those numbers that are exactly divisible by 2.

The remainder operator % gives the remainder when used with a number. For example,

const number = 6; const result = number % 4; // 2

Hence, when % is used with 2, the number is even if the remainder is zero. Otherwise, the number is odd.

Example 1: Using if...else

// program to check if the number is even or odd // take input from the user const number = prompt("Enter a number: "); //check if the number is even if(number % 2 == 0) { console.log("The number is even."); } // if the number is odd else { console.log("The number is odd."); }

Output

Enter a number: 27 The number is odd.

In the above program, number % 2 == 0 checks whether the number is even. If the remainder is 0, the number is even.

In this case, 27 % 2 equals to 1. Hence, the number is odd.

The above program can also be written using a ternary operator.

Example 2: Using Ternary Operator

// program to check if the number is even or odd // take input from the user const number = prompt("Enter a number: "); // ternary operator const result = (number % 2 == 0) ? "even" : "odd"; // display the result console.log(`The number is ${result}.`);

Output

Enter a number: 5 The number is odd.