No history yet

Understanding Arithmetic Operators

The Building Blocks of Calculation

At its heart, a lot of programming is about working with numbers. To do that, we use special symbols called arithmetic operators. These are the familiar tools you've used in math class for years, just with a programming twist. They let you perform calculations, from simple sums to the logic that powers complex applications.

Let's start with the basics. The addition operator is the plus sign (+). It does exactly what you'd expect: it adds two numbers together.

let totalApples = 5 + 3; // totalApples is now 8

Similarly, the subtraction operator, the minus sign (-), finds the difference between two numbers.

let moneyLeft = 20 - 12; // moneyLeft is now 8

When it comes to multiplication, programmers use an asterisk (*) instead of the traditional '×' symbol. It calculates the product of two numbers.

let area = 4 * 6; // area is now 24

For division, we use a forward slash (/). It divides one number by another and gives you the quotient.

let slicePerPerson = 12 / 4; // slicePerPerson is now 3

Finding the Remainder

Now for an operator that might be new: the modulus operator (%). This operator doesn't give you the result of a division, but instead gives you what's left over. It finds the remainder.

Imagine you have 10 cookies to share equally among 3 friends. Each friend gets 3 cookies, and you have 1 cookie left over. The modulus operator tells you the value of that leftover part.

let leftoverCookies = 10 % 3; // leftoverCookies is now 1

The modulus operator is incredibly useful for tasks like checking if a number is even or odd. If a number % 2 is 0, the number is even. If it's 1, the number is odd.

Operator Summary

Here's a quick recap of the five basic arithmetic operators.

OperatorNameExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division7 / 23.5
%Modulus7 % 21

Ready to test your knowledge? Give these questions a try.

Quiz Questions 1/5

In most programming languages, which symbol represents the multiplication operator?

Quiz Questions 2/5

What is the primary function of the modulus operator (%)?

Understanding these operators is the first step toward performing any kind of calculation in code.