How to check if a number is odd or even in JavaScript

In JavaScript, determining whether a number is even or odd is a simple task that can be accomplished with a few lines of code. In this Answer, we will explore the different methods available for checking whether a number is even or odd in JavaScript.

Method 1: Using the modulus operator (%)

The modulus operator (%) is used to obtain the remainder when one number is divided by another. This operator can be used to determine whether a number is odd or even by determining whether the remainder after dividing it by two equals zero or not. It is even if it equals 0. Otherwise, it is odd.

//check if the number is even
function isEven (number) {
return number % 2 === 0;
}
//check if the number is odd
function isOdd (number) {
return number % 2 !== 0;
}
//test
console.log(isEven(5)); //false
console.log(isEven(4)); //true
console.log(isOdd(5)); //true
console.log(isOdd(4)); //false

In the code above, the isEven() function checks whether a number is even by using the modulus operator to check whether the remainder of dividing the number by 2 is equal to 0 (zero).

The isOdd() function checks whether a number is odd by using the modulus operator to check whether the remainder of dividing the number by 2 is not equal to 0 (zero).

Method 2: Using bitwise AND operator (&)

Another way to determine whether a number is even or odd is by using the bitwise AND operator (&). When this operator is applied to a number and 1, the result is 0 if the number is even and 1 if the number is odd.

//check if the number is even
function isEven (number) {
return (number & 1) === 0;
}
//check if the number is odd
function isOdd (number) {
return (number & 1) === 1;
}
//Test
console.log(isEven(5)); //false
console.log(isEven(4)); //true
console.log(isOdd(5)); //true
console.log(isOdd(4)); //false

In the code above, the isEven() function uses the bitwise AND operator to check whether the last bit of the number is 0, which indicates that the number is even.

The isOdd() function uses the bitwise AND operator to check whether the last bit of the number is 1, which indicates that the number is odd.

Method 3: Using Math.floor() and division(/)

A third way to determine whether a number is even or odd is by using the Math.floor() function and division (/). If we divide a number by 2 and then take the floor of the result, we get the integer part of the result. If the integer part is the same as the original number, then the number is even. Otherwise, it is odd.

function isEven (number) {
return Math.floor(number / 2) * 2 === number;
}
function isOdd (number) {
return Math.floor(number / 2) * 2 !== number;
}
//Test
console.log(isEven(5)); //false
console.log(isEven(4)); //true
console.log(isOdd(5)); //true
console.log(isOdd(4)); //false

In the above code, the isEven() function divides the number by 2, takes the floor of the result, and then multiplies the result by 2 to check whether it is equal to the original number. If it is, then the number is even.

The isOdd() function uses the same method but checks whether the result is not equal to the original number, which indicates that the number is odd.

Conclusion

In this Answer, we have explored different methods for checking whether a number is even or odd in JavaScript. Since each approach has its own advantages and disadvantages, you can make a decision based on the specific requirements of your project.

Copyright ©2024 Educative, Inc. All rights reserved