CodeBlanks Logo Icon CodeBlanks

Arithmetic operators

Learn basic arithmetic with +, -, *, / and %.

Arithmetic operators

In JavaScript, arithmetic operators perform basic math on numbers: + (addition), - (subtraction), * (multiplication), / (division) and % (modulo, remainder).

For example, 10 + 3 returns 13, 10 / 3 returns 3.333..., 10 % 3 returns 1. Modulo gives the remainder after division; very useful for odd/even checks.

These operators also work with variables: price * qty.

Modulo and exponent

The modulo operator is written with % and gives the remainder after division. For example, 7 % 3 returns 1 (7 = 2*3 + 1).

This is ideal for checking whether a number is odd or even: n % 2 === 0 means even, n % 2 === 1 means odd.

For exponentiation, use **: 2 ** 3 returns 8. In old code you may see Math.pow(2, 3).

Practice Arithmetic calculation

Declare a = 10 and b = 3. Compute sum, product and remainder as a + b, a * b, a % b respectively. Print each with console.log.

Starter code

index.js
const a = 10;
const b = 3;
// Compute sum, product, remainder variables
// Log all three with console.log

Sample solution

index.js
const a = 10;
const b = 3;
const sum = a + b;
const product = a * b;
const remainder = a % b;
console.log(sum);
console.log(product);
console.log(remainder);

Expected output

13
30
1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does 7 % 3 return?

  • 10
  • 2.33
  • 0
  • 1 Correct answer

Explanation: 7 = 2*3 + 1, remainder is 1.

2 Addition example

Question
const sum = 10 ____ 5;
Answer
const sum = 10 + 5;

Explanation: is subtraction, * is multiplication, / is division.

3 Match the operators

Term Meaning
+ Addition
- Subtraction
* Multiplication
% Modulo (remainder)

All lessons in Beginner