Write a Javascript Program to Check if a Number is Odd or Even

In JavaScript, you can determine whether a number is odd or even by using the modulo operator.

The modulo operator, represented by the percent sign (%), returns the remainder of a division operation.

If a number is even, it will have no remainder when divided by 2. Conversely, if a number is odd, it will have a remainder of 1 when divided by 2.

Here’s an example program that checks whether a given number is odd or even:

function checkOddOrEven(num) {
  if (num % 2 === 0) {
    console.log(num + " is even");
  } else {
    console.log(num + " is odd");

In this program, the checkOddOrEven function takes a single parameter, num, which is the number that we want to check.

Inside the function, we use an if statement to check whether num is even or odd.

The if statement uses the modulo operator to calculate the remainder of num divided by 2.

If the remainder is 0, then num is even and we log a message to the console indicating that fact.

If the remainder is 1, then num is odd and we log a different message to the console.

You can call this function with any number as the argument, like this:

checkOddOrEven(4); // logs "4 is even" to the console
checkOddOrEven(7); // logs "7 is odd" to the console

That’s all there is to it! With this simple program, you can quickly determine whether any number is odd or even using JavaScript.