Javascript program to check given number is Even or Odd

In this javascript example we will write how to a program to check a given number is even or odd. We can check number is even or odd in javascript by two ways

  • Using If Else Condition
  • Using Ternary Operator


Using If-Else condition to check given number even or odd

// Ask user to enter a number
const num = prompt("Enter any number: ");
//check for even number
if(num % 2 == 0) {
    console.log("Given number is an even number.");
// check for odd number
else {
    console.log("Given number is an odd number.");

In the above example prompt the user to enter a number

Then converting it to integer

Now to check the number even we are divide the number with 2 and if the remainder '0' then number is Even otherwise 'odd'




Given number is an odd number.


Using Ternary Operator

Ternary JavaScript operator which will takes three operands
rather than the one or two that most operators use. It use shorten a simple if else block in JavaScript programs.

Let's check example

// Ask user to enter a number
const num = prompt("Enter any number: ");
// apply ternary operator
const output = (num % 2  == 0) ? "even" : "odd";
// display result on console
console.log(`This is an ${output} number.`);





This is an even number.


Subscribe For Daily Updates

100+ Python Pattern Examplespython pattern examples - star patterns, number patterns