If you building a website in which you want to check if a number is even or odd then there are a few methods available in JavaScript that you can use to find out odd and even numbers.
In JavaScript, to see if a number is even, just check if it divides evenly by 2 using the `%` operator. If there’s no remainder, it’s Even.
Here is a function you can create using the modulo operator:
function isEven(number) {
return number % 2 === 0;
}
// Example usage:
console.log(isEven(4)); // Output: true
console.log(isEven(7)); // Output: false
I have mentioned three simple JavaScript methods that you can use to find out the odd and even numbers.
Well, there are numerous methods available in JavaScript to find out even numbers.
If you ask 10 developers to create a function that does the same thing in JavaScript, you’ll likely get 10 different methods. That’s because JavaScript offers endless possibilities for achieving the same outcome.
After experimenting with many methods and functions available in the latest ES6 JavaScript I providing you with these three simple methods.
1. Modulo Operator (%)
If you have a basic understanding of mathematics, you might know that when you divide a number by 2, if the remainder is 0, then the number is even.
This is because even numbers can be evenly divided by 2, leaving no remainder.
We can make a simple function that checks if a number is even by using a basic trick: we divide the number by 2 and see if there’s anything left over.
If there’s nothing left over (meaning the remainder is 0), the number is even. Otherwise, it’s odd.
Here is the code for that function:
function isEven(number) {
return number % 2 === 0;
}
// Example usage:
console.log(isEven(4)); // Output: true
console.log(isEven(7)); // Output: false
Also See: How to Shuffle an Array in JavaScript? [Step-by-Step Guide]
2. Using Bitwise AND Operator (&)
Another easy method is to use the Bitwise and AND (&) Operator of JavaScript to check the last bit of any provided number.
Here is a simple logic behind this method:
In JavaScript, the last bit of an even number is always 0 in its binary representation.
This means you can use the bitwise AND operator & with 1 to check if the last bit is 0.
If it is zero then the number is even otherwise the number is odd.
You can create a simple for this method, here is the code for that:
function isEven(number) {
return (number & 1) === 0;
}
// Example usage:
console.log(isEven(4)); // Output: true
console.log(isEven(7)); // Output: false
3. Using ES6 Arrow Function (=>) and Ternary Operator (?)
Lastly, if you looking for a function that you can write in one line of code then you can use this method.
Ternary Operator works similarly to the If Else statement
but requires a small amount of code.
We can take the idea from the first method I mentioned and tweak it using the ES6 Arrow Function and Ternary Operator to make it shorter.
Let’s see how we can check if a number is even with some code:
const isEven = number => number % 2 === 0 ? true : false;
// Example usage:
console.log(isEven(4)); // Output: true
console.log(isEven(7)); // Output: false
There are many more methods for find even numbers but I do not want to use so many lines of code when I can create an easy function with few lines of code.
Also, I’m sure you do not want to spend your time reading a long post about how to check if a number is even in Javascript that is why I created this show and simple post.
If you’re using Next.js for building websites, you might want to check out these other posts where I explain How to use localstorage in Next.js or How to add Sitemap in Next.js.