In this article, we are going to talk about numbers, odd and even numbers with some JavaScript.
Let's gets started,
- Choose a random number
let number = 24
- As we know this number is even, but let's check this with JavaScript
if(number % 2 === 0){
console.log(`${number} is an even number`)
}else{
console.log(`${number} is odd number`)
}
- This gives the following output,
// 24 is an even number
This is how you can determine odd / even numbers using JavaScript.
By the way, I have a tutorial on it so make sure to check that out
Top comments (6)
If you care about performance, it's actually quicker to use a bitwise test:
I've never really understood bitwise operators.
Nifty! What other ways might you determine if a number is even or odd?
How might you use this to determine if a number is divisible by 5?
To Determine number is divisible by 5, you can try
Nice! Makes sense!
Works like a charm I use this code often when trying to separate odd from even.