Are you looking for ways to determine if a number is even or odd in JavaScript?
Don’t worry, we’ve got you covered! With just a few lines of code, we can show you how to easily check if a number is even or odd in JavaScript.
In this article, we’ll explore various methods for determining the parity of a number using JavaScript. So, if you want to improve your JavaScript skills, keep reading!
What is Even Number?
An even number is an integer that is divisible by 2 with no remainder.
In other words, when an even number is divided by 2, the result is an integer.
For instance, 4, 6, and 8 are all even numbers because they can be divided by 2 with no remainder.
What is Odd? Number
An odd number is an integer that is not divisible by 2. When an odd number is divided by 2, the result is not an integer.
For instance, 3, 5, and 7 are all odd numbers because they cannot be divided by 2 with no remainder.
In simple words, even numbers are divisible by 2 while odd numbers are not.
How to check if a number is even or odd in JavaScript?
You can check if a number is even or odd in JavaScript by using the modulo operator (%).
It returns the remainder when one number is divided by another.
If a number is divisible by 2 with no remainder, it is even. Otherwise, it is odd.
Here’s an example of how you can use the modulo operator to check if a number is even or odd in JavaScript:
function isEvenOrOdd(num) {
if (num % 2 === 0) {
return 'even';
} else {
return 'odd';
}
}
console.log(isEvenOrOdd(20));
console.log(isEvenOrOdd(15));
As you can see, in this example, the isEvenOrOdd function takes a number as an argument and returns ‘even’ if the number is divisible by 2 with no remainder, and ‘odd’ otherwise.
We can then use this function to check if a number is even or odd by passing it as an argument to the console.log function.
Output:
(20) even
(15)
odd
Different ways on how to check if a number is even or odd in JavaScript?
Aside from using the modulo operator (%), there are more useful different methods to check if a number is even or odd in JavaScript.
Here are some of the most common methods:
Using bitwise AND (&)
The bitwise AND operator compares each bit of two numbers and returns a new number where each bit is set to 1 if the corresponding bits of both numbers are 1, and 0 otherwise.
If the least significant bit (the rightmost bit) of a number is 1, the number is odd. Otherwise, it is even.
Here’s an example of how you can use the bitwise AND operator to check if a number is even or odd in JavaScript:
function isEvenOrOdd(num) {
if (num & 1) {
return 'odd';
} else {
return 'even';
}
}
console.log(isEvenOrOdd(4));
console.log(isEvenOrOdd(5));
Output:
(4) even
(5) odd
Using a conditional (ternary) operator( ?:
)
The conditional operator returns one of two values based on a condition.
You can use this operator in combination with the modulo operator to check if a number is even or odd in JavaScript.
Here’s an example:
function isEvenOrOdd(num) {
return num % 2 === 0 ? 'even' : 'odd';
}
console.log(isEvenOrOdd(10));
console.log(isEvenOrOdd(11));
Output:
(10) even
(11)
odd
Using the Math.abs() function
The Math.abs() function returns the absolute value of a number.
You can use this function in combination with the modulo operator to check if a number is even or odd in JavaScript, even if the number is negative.
Here’s an example:
function isEvenOrOdd(num) {
return Math.abs(num) % 2 === 0 ? 'even' : 'odd';
}
console.log(isEvenOrOdd(-12));
console.log(isEvenOrOdd(-13));
Output:
(-12) even
(-13) odd
Conclusion
In conclusion, determining whether a number is even or odd in JavaScript is a straightforward task.
You can use various methods, including the modulo operator (%), bitwise AND operator (&), conditional (ternary) operator (?:), and the Math.abs() function, depending on your specific requirements.
Each method offers a reliable way to identify the parity of a number in JavaScript, helping you improve your coding skills and handle different scenarios efficiently.
We are hoping that this article provides you with enough information. That will help you understand the JavaScript even or odd.
If you want to dive into more JavaScript topics, check out the following articles:
Thank you for reading Itsourcecoders 😊.