Check if number is odd or even in JavaScript
function isOdd(number) {
return Math.abs(number % 2) === 1;
}
function isEven(number) {
return number % 2 === 0;
}
In JavaScript, you can easily check whether an integer is even or odd by using the remainder / modulo %
operator.
If a number divided by 2 gives a remainder of 1
, it is odd, if it gives a remainder of 0
, it is even. You can implement this simply as follows:
function isOdd(number) {
return number % 2 === 1;
}
function isEven(number) {
return number % 2 === 0;
}
This is quite simple, but unfortunately not always appropriate. For example, it does not work correctly for negative numbers:
console.log(isOdd(-5)); // false but it should be true
To handle negative numbers, you just need to modify the isOdd
function to take the absolute value of the result.
function isOdd(number) {
return Math.abs(number % 2) === 1;
}
Another problem that may arise with JavaScript is that our input parameter is not a number. Therefore, you may want to first check if the input value is a number
and if not then return undefined
instead of true
or false
:
function isOdd(number) {
if (Number.isFinite(number) === false) {
return undefined;
}
return Math.abs(number % 2) === 1;
}
However, this is still not perfect. In JavaScript, the value of a form input field is always a string
, even if its type
is set to number
. Let's look at the following example:
<input type="text" name="name" id="input-field" />
And the JavaScript code to handle the input:
document.addEventListener("DOMContentLoaded", () => {
const input = document.querySelector("#input-field");
input.addEventListener("change", (event) => {
const userInput = event.target.value;
console.log(userInput + ' is odd? ' + isOdd(userInput));
});
});
In this case, even if the user enters a valid number, the Number.isFinite
check will result that the return value of our function will always be undefined.
For this reason, you also need to include a conversion to a number in the code:
function isOdd(number) {
if (Number.isFinite(parseInt(number)) === false) {
return undefined;
}
return Math.abs(number % 2) === 1;
}
Finally, this is what the two functions look like in TypeScript format:
function isOdd(number:number):boolean|undefined {
if (Number.isFinite(parseInt(number)) === false) {
return undefined;
}
return Math.abs(number % 2) === 1;
}
function isEven(number:number):boolean|undefined {
return number % 2 === 0;
}
- Check if number is odd or even in JavaScript
- Round a number in JavaScript
- Get the absolute value of a number in JavaScript
- Generate random string in JavaScript
- Generate random date with JavaScript
- Generate a random number with fixed length using JavaScript
- Generate a random number in a range with JavaScript
- Get the minimum value in an array using JavaScript
- Get the maximum value in an array using JavaScript
- Convert a float number to integer in JavaScript
- Round a number to the nearest 10 using JavaScript
- Round down a number in JavaScript
- Round up a number in JavaScript