## How to Use the JavaScript Modulo Operator

Have you ever seen a percentage sign accompanied by a few numbers in JavaScript? You’ve maybe thought that it is something to do with calculating percentages. That’s not the case. In JavaScript, the percentage sign is used to calculate remainders.

The percentage sign (%) is the modulo operator. It divides two numbers and calculates the remainder left over, if any. It’s commonly used to find out if a number is odd or even.

In this guide, we’re going to discuss how to use the JavaScript modulo operator. We’ll walk through an example of this operator in action to help you get started.

## What is the JavaScript Modulo Operator?

Modulo is another word for a remainder operator.

**Take this quiz to get offers and scholarships from top bootcamps and online schools!**

Not all numbers can be evenly divided. 9 cannot be divided by 4 without yielding a decimal number. If you’re looking for precision, a decimal number is fine. There are some cases where you’ll want to know how many numbers are left over from a calculation.

The modulo operator tells you what is left over when a number has been divided as many times as it can be into another number. The remainder of 9 divided by 4 is 1. 4 goes into 9 twice. There is 1 left over.

The modulo operator is also called the modulus operator.

Let’s create a program which tells us how much money will be left over if we spend all of our money on video games. We’ll start by declaring two variables:

var game_cost = 40; var balance = 122;

The first variable stores the cost of a game. The second variable stores how much money we have in our bank account.

Let’s use the modulo operator to find out how much we would have left over if we spent all our money on games:

console.log(balance % game_cost);

Our code returns: 2. This tells us that if we spend all of our money on video games, we will have $2 left over.

We can use the integer division operator to calculate how many games we can buy:

console.log(Math.round((balance / game_cost), 0));

We have used the division operator (/) to calculate how many games we can buy. We have then rounded this value to the nearest whole value. This is because you cannot buy a portion of a game; you have to buy a whole game.

Our code returns: 3.

These two operations combined tell us that we can afford three video games. We will have $2 left over after we have bought these games.

## Check if a Number is Odd or Even

The modulo operator is used to check if a number is odd or even.

We’re going to build an application that tells us whether we can purchase an even or odd number of candy bars with the money we have. Let’s start by declaring variables which track the cost of a candy bar and how much money we have:

var cost = 1.50; var balance = 3.50;

We’ll then use the division operator to find out how many we can buy:

var can_buy = Math.round((balance / cost), 0); console.log(can_buy);

This code returns: 2. This tells us we can afford to purchase two full candy bars. Next, we can use the modulo operator to check if the number of candy bars we can buy is odd or even:

if (can_buy % 2 != 0) { console.log("You can buy an even number of candy bars."); } else { console.log("You can buy an odd number of candy bars."); }

Our code uses the modulo operator to check if there is any remainder left over after dividing the number by two. If there is, the number is odd; otherwise, the number is even.

Our code returns: You can buy an even number of candy bars. The value of “can_buy” is 2. This is an even number so the code inside our `if`

statement runs.

## Conclusion

The modulo operator calculates the remainder left over after dividing two numbers. It is commonly used with an `if`

statement to evaluate whether a number is odd or even.

Now you’re ready to start using the JavaScript modulo operator like an expert!