Menu
Coddy logo textTech

Modulo Operator

Part of the Fundamentals section of Coddy's JavaScript journey — lesson 13 of 77.

The modulo operator % tells you what's left over after dividing one number by another.

result = dividend % divisor
  • dividend: The number being divided.
  • divisor: The number that divides the dividend.
  • result: The remainder of the division.

For example,

result = 10 % 3

Here, 10 is divided by 3. 3 goes into 10 three times, with a remainder of 1. So, result will be 1.

Usually modulo is used for checking if a number is even or odd:

  • If a number is even, dividing it by 2 will leave a remainder of 0.
  • If a number is odd, dividing it by 2 will leave a remainder of 1.
challenge icon

Challenge

Beginner

Write a code that initializes three variables, a, b and c with the values 9, 2.6, and 11 (respectively).

After that, initialize the following variables:

  • d that will hold the result of a modulo 2 
  • e that will hold the result of a modulo 3
  • f that will hold the result of c modulo 10

Check out the result and see how different dividends and divisors affect the result.

Cheat sheet

The modulo operator % returns the remainder after dividing one number by another:

result = dividend % divisor

Example:

result = 10 % 3  // result is 1

Common use case - checking if a number is even or odd:

  • Even numbers: number % 2 returns 0
  • Odd numbers: number % 2 returns 1

Try it yourself

// Type your code below


// Don't change the line below
console.log(`a = ${a}`)
console.log(`b = ${b}`)
console.log(`c = ${c}`)
console.log(`d = ${d}`)
console.log(`e = ${e}`)
console.log(`f = ${f}`)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals