In JavaScript, you can use the modulus operator (%) to determine if a number is odd or even. The modulus operator returns the remainder when one number is divided by another. If the remainder is 0, the number is even. If the remainder is not 0, the number is odd.

Here’s an example of how you can use the modulus operator to determine if a number is even or odd in JavaScript:

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

Alternatively, you can use bitwise AND operator (&) to determine if a number is odd or even, because the last bit of an even number is 0, and the last bit of an odd number is 1.

if (number & 1) {
  console.log(number + " is odd");
} else {
  console.log(number + " is even");
}

Also Read:

Categorized in: