JavaScript Remainder Operator : A Complete Tutorial with Examples

The remainder operator (%) in JavaScript returns the remainder left over when one number is divided by another.

It is commonly referred to as the modulus operator. This operator is useful in a variety of scenarios, such as checking for even or odd numbers, cycling through elements, and performing modular arithmetic.

Syntax

remainder = dividend % divisor;

dividend: The number to be divided.
divisor: The number by which you divide the dividend.
remainder: The remainder of the division.

Basic Examples

Example 1: Basic Remainder Calculation

let a = 10;
let b = 3;
let remainder = a % b;

console.log(remainder); // Output: 1

Explanation

Here, 10 % 3 performs the division of 10 by 3, resulting in a quotient of 3 with a remainder of 1.
The remainder (1) is what gets stored in the variable remainder.

Example 2: Check for Even or Odd Numbers

One of the most common uses of the remainder operator is to check if a number is even or odd.

let number = 15;

if (number % 2 === 0) {
  console.log(`${number} is even.`);
} else {
  console.log(`${number} is odd.`);
}

Explanation

number % 2 divides number by 2.
If the remainder is 0, the number is even; otherwise, it is odd.
Here, 15 % 2 equals 1, so the output is 15 is odd.

Example 3: Cycling Through Array Elements

The remainder operator is handy when you want to cycle through an array using an index that wraps around once it reaches the end of the array.

let colors = ['red', 'green', 'blue'];
let index = 5;
let wrappedIndex = index % colors.length;

console.log(colors[wrappedIndex]); // Output: "green"

Explanation

The colors array has 3 elements, so colors.length is 3.
index % colors.length calculates 5 % 3, which is 2.
colors[2] corresponds to “green”, so it wraps the index around to pick the element within the array's bounds.

Advanced Examples

Example 4: Finding the Last Digit of a Number

You can use the remainder operator to extract the last digit of an integer.

let number = 12345;
let lastDigit = number % 10;

console.log(lastDigit); // Output: 5

Explanation

12345 % 10 divides 12345 by 10, leaving a remainder of 5.
This effectively extracts the last digit of the number.

Example 5: Checking for Multiples

The remainder operator can be used to check if a number is a multiple of another.

let number = 30;
let divisor = 5;

if (number % divisor === 0) {
  console.log(`${number} is a multiple of ${divisor}.`);
} else {
  console.log(`${number} is not a multiple of ${divisor}.`);
}

Explanation

number % divisor checks if there is a remainder when number is divided by divisor.
If the remainder is 0, then number is a multiple of divisor.
Here, 30 % 5 equals 0, so the output is “30 is a multiple of 5.”.

Example 6: Limiting a Number Within a Range

The remainder operator can be used to wrap numbers within a specific range, useful in various game or simulation scenarios.

let num = 15;
let rangeLimit = 7;
let wrappedNumber = num % rangeLimit;

console.log(wrappedNumber); // Output: 1

Explanation

15 % 7 divides 15 by 7, resulting in a quotient of 2 and a remainder of 1.
The remainder 1 effectively wraps num within the range of 0 to 6.

Example 7: Negative Numbers and Remainders

In JavaScript, when using the remainder operator with negative numbers, the sign of the remainder follows the sign of the dividend.

let a = -10;
let b = 3;
let remainder = a % b;

console.log(remainder); // Output: -1

Explanation

-10 % 3 results in -1 because the sign of the remainder follows the sign of the dividend (-10).

Example 8: Cycling through Days of the Week

Let's say you want to find the day of the week that falls a certain number of days from now. This is a great use of the remainder operator.

let daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let todayIndex = 2; // Let's say today is Tuesday (index 2)
let daysToAdd = 10;

let futureDayIndex = (todayIndex + daysToAdd) % daysOfWeek.length;

console.log(`The day of the week in ${daysToAdd} days will be: ${daysOfWeek[futureDayIndex]}`); // Output: "Friday"

Explanation

We have an array daysOfWeek representing the days of the week.
futureDayIndex is calculated using (todayIndex + daysToAdd) % daysOfWeek.length.
This cycles through the days of the week correctly, wrapping around using the remainder operator.

Summary

The remainder operator (%) is a powerful tool in JavaScript that is useful for various tasks, including:

Determining if numbers are even or odd.
Cycling through indices (like in arrays).
Extracting digits or limiting numbers within a specific range.
Handling cycles, such as days of the week or other periodic patterns.

By understanding how to use the remainder operator, you can simplify many common tasks in your JavaScript programming. Experiment with these examples to get a feel for its versatility!

Related posts

JavaScript typeof Operator Tutorial with Examples

JavaScript Grouping Operator Tutorial with Examples

JavaScript Comma Operator Tutorial with Examples