Site icon Mr Programmer

JavaScript Program to Check if a Number is Odd or Even | CSOTD #3

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

Author

  • Founder & CEO of MrProgrammer.in | I help Learning Freaks to Step into the World of Programming and Our Mission is to Provide High Quality and Valuable Content to Our Users! We believe that everyone has the potential to learn to code, and we are committed to making that happen. Our courses are designed to be engaging and interactive, and we offer a variety of learning paths to meet the needs of all learners. We are also committed to providing our learners with the support they need to succeed, and we offer a variety of resources to help learners learn at their own pace. If you are interested in learning to code, we encourage you to check out our courses. We are confident that you will find our content to be high-quality, valuable, and makes sense.

Exit mobile version