Sometimes, we want to check if one number is a multiple of another with JavaScript.
In this article, we’ll look at how to check if one number is a multiple of another with JavaScript.
How to check if one number is a multiple of another with JavaScript?
To check if one number is a multiple of another with JavaScript, we can use the % operator check if one number divided by the other has a remainder of 0.
For instance, we write:
const x = 8
const y = 2
const remainder = x % y;
if (remainder === 0) {
//x is a multiple of y
} else {
//x is not a multiple of y
}
We get the remainder of x divided by y with the % operator and assign the returned remainder to remainder.
If remainder is 0, then xis a multiple ofy`.
Otherwise, x isn’t a multiple of y.
Conclusion
To check if one number is a multiple of another with JavaScript, we can use the % operator check if one number divided by the other has a remainder of 0.