Write a JavaScript program to check even or odd number

Description: Check whether a number is even or odd.

Hint:

  • A number is even if the remainder after dividing by 2 is 0.

JavaScript program for even or odd

function checkEvenOdd(n) {
  if (n % 2 === 0) {
    return n + " is even";
  }
  return n + " is odd";
}

console.log(checkEvenOdd(4));
console.log(checkEvenOdd(7));

Output:

Tutorials Class - Output Window

4 is even
7 is odd

How it works

  • % gives the remainder: 4 % 2 is 0 and 7 % 2 is 1.
  • The function returns a text for each case.

Read Conditional Statements.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.