Skip to content
Home » Blog » JavaScript Program to Check if a Number is Odd or Even | CSOTD #3

JavaScript Program to Check if a Number is Odd or Even | CSOTD #31 min read

Check if a Number is Even or Odd

Today’s code snippet is a JavaScript Program to Check if a Number is Odd or Even:

Code:

function isEven(number) {
  return number % 2 === 0;
}

// Example usage:

const number = 10;
const isEven = isEven(number);

console.log(isEven); // true

Explanation:

The First line of code defines a function called isEven(), which takes a number as input and returns a Boolean value indicating whether the number is even or odd.

The second line of code declares a variable called number and assigns it the value 10.

The Third Line of Code calls the isEvent() function with the variable number as the argument and assigns the result to the variable isEven .

The fourth line of code logs the value of the variable isEven() to the console.

The % operator in the isEven() function is the modulo operator. It returns the remainder of the division between two numbers. In this case, we are dividing the number by 2. If the remainder is 0, then the number is divisible by 2 and is therefore even. Otherwise, the number is not divisible by 2 and is therefore odd.

So, the isEven() function returns true if the number is even and false if the number is odd.

In the above example usage, the variable number is assigned the value 10. When the isEven() function is called with the variable isEven is assigned the value true .

When the value of the variable isEven is logged to the console, it prints the following output:

true

Also Read: Find the Largest Number in an Array | CSOTD #2

Tanmay Sinha

Leave a Reply