Check if one Number is Multiple of another in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# Check if One Number is Multiple of Another

Use the modulo % operator to check if one number is a multiple of another number.

The operator returns the remainder when one number is divided by another number. The remainder will only be zero if the first number is a multiple of the second.

index.js
const num1 = 15; const num2 = 5; const remainder = num1 % num2; console.log(remainder); // ๐Ÿ‘‰๏ธ 0 if (remainder === 0) { // โœ… This runs console.log('๐Ÿ‘‰๏ธ num1 is a multiple of num2'); } else { console.log('โ›”๏ธ num1 is NOT a multiple of num2'); }

check if one number is multiple of another

The code for this article is available on GitHub

We used the modulo operator to get the remainder of dividing one number by another.

If the remainder after dividing the first by the second number is 0, then the first number is a multiple of the second.

Here are some more examples of using the modulo % operator.

index.js
console.log(13 % 5); // ๐Ÿ‘‰๏ธ 3 console.log(12 % 5); // ๐Ÿ‘‰๏ธ 2 console.log(11 % 5); // ๐Ÿ‘‰๏ธ 1 console.log(10 % 5); // ๐Ÿ‘‰๏ธ 0

The number 10 is a multiple of 5, so the division has a remainder of 0.

The modulo % operator always takes the sign of the first number (the dividend).

index.js
console.log(-13 % 5); // ๐Ÿ‘‰๏ธ -3 console.log(12 % -5); // ๐Ÿ‘‰๏ธ 2

You might be wondering what happens if we get a remainder of -0.

Would the if condition fail because we only check for 0.

index.js
console.log(-15 % 3); // ๐Ÿ‘‰๏ธ -0

However, 0 is equal to -0 in JavaScript so our condition would still work.

index.js
console.log(0 === -0); // ๐Ÿ‘‰๏ธ true

# Creating a reusable function

If you need to check if a number is a multiple of another number often, extract the logic into a reusable function.

index.js
function checkIfMultiple(num1, num2) { return num1 % num2 === 0; } console.log(checkIfMultiple(15, 5)); // ๐Ÿ‘‰๏ธ true console.log(checkIfMultiple(15, 4)); // ๐Ÿ‘‰๏ธ false console.log(checkIfMultiple(15, 3)); // ๐Ÿ‘‰๏ธ true if (checkIfMultiple(15, 5)) { // โœ… This runs console.log('๐Ÿ‘‰๏ธ num1 is a multiple of num2'); } else { console.log('โ›”๏ธ num1 is NOT a multiple of num2'); }

defining reusable function

The code for this article is available on GitHub

The checkIfMultiple() function takes 2 numbers as parameters and checks if the first number is a multiple of the second.

# Checking if a number is not a multiple of another number

If you need to check if a number is not a multiple of another number, check if there is a remainder after using the modulo operator.

index.js
function checkIfNotMultiple(num1, num2) { return num1 % num2 !== 0; } console.log(checkIfNotMultiple(15, 5)); // ๐Ÿ‘‰๏ธ false console.log(checkIfNotMultiple(15, 4)); // ๐Ÿ‘‰๏ธ true console.log(checkIfNotMultiple(15, 3)); // ๐Ÿ‘‰๏ธ false if (checkIfNotMultiple(15, 5)) { console.log('โ›”๏ธ num1 is NOT a multiple of num2'); } else { // โœ… This runs console.log('๐Ÿ‘‰๏ธ num1 is a multiple of num2'); }

check if number is not multiple of another number

The checkIfNotMultiple() function takes 2 numbers as parameters and checks if the first number is not a multiple of the second.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev