How to Check if a Number is a Multiple of Another Number in JavaScript
In programming, you often need to determine if a number can be evenly divided by another. This is essential for tasks like checking if a number is even or odd, creating grid layouts, or solving a wide range of mathematical problems. The most direct and efficient way to check if one number is a multiple of another in JavaScript is by using the modulo operator (%).
This guide will teach you how to use the modulo operator to check for multiples, how to wrap this logic in a reusable function, and how to apply this concept to the common task of checking for even or odd numbers.
The Core Method: The Modulo Operator (%)
The modulo operator (%) returns the remainder of a division operation. This is the key to solving our problem.
The logic is simple: If numberA is a multiple of numberB, then dividing numberA by numberB will result in a remainder of exactly 0.
numberA % numberB === 0; // Returns true if numberA is a multiple of numberB
We use the strict equality operator (===) to check if the remainder is 0.
Basic Example: Checking for a Multiple
Let's check if the number 15 is a multiple of 5.
Problem: we need to determine if num1 is evenly divisible by num2.
// Problem: Is num1 a multiple of num2?
const num1 = 15;
const num2 = 5;
Solution: by checking the remainder, we can get a clear boolean answer.
const num1 = 15;
const num2 = 5;
if (num1 % num2 === 0) {
// This block will run
console.log(`${num1} is a multiple of ${num2}.`);
} else {
console.log(`${num1} is NOT a multiple of ${num2}.`);
}
// Let's try with a number that isn't a multiple
const num3 = 16;
if (num3 % num2 === 0) {
console.log(`${num3} is a multiple of ${num2}.`);
} else {
// This block will run
console.log(`${num3} is NOT a multiple of ${num2}.`);
}
Output:
15 is a multiple of 5.
16 is NOT a multiple of 5.
This check works reliably even with negative numbers, as JavaScript correctly treats 0 and -0 as equal (0 === -0).
Creating a Reusable Function (Best Practice)
For cleaner, more readable code, it's a good practice to encapsulate this logic in a reusable function.
For example, this function takes two numbers and returns a clear true or false value.
function isMultipleOf(number, multiple) {
return number % multiple === 0;
}
// Example Usage:
console.log(isMultipleOf(20, 5)); // Output: true
console.log(isMultipleOf(20, 3)); // Output: false
console.log(isMultipleOf(100, 10)); // Output: true
Practical Use Case: Checking for Even or Odd Numbers
A perfect real-world application of this principle is determining if a number is even or odd.
- A number is even if it is a multiple of 2.
- A number is odd if it is not a multiple of 2.
We can use our isMultipleOf logic to create simple and highly readable functions for this.
function isEven(number) {
return number % 2 === 0;
}
function isOdd(number) {
return number % 2 !== 0;
}
// Example Usage:
console.log(isEven(10)); // Output: true
console.log(isEven(7)); // Output: false
console.log(isOdd(7)); // Output: true
console.log(isOdd(10)); // Output: false
Conclusion
Checking if one number is a multiple of another is a fundamental mathematical operation that is made simple in JavaScript by the modulo operator.
- The modulo operator (
%) is the definitive tool for this task. - The core logic is a simple check:
number % multiple === 0. - This principle can be easily extended to solve common related problems, such as determining if a number is even or odd.
By wrapping this logic in a reusable function, you can create clean, descriptive, and error-free code for any task that requires this check.