Lesson 3 of 22 Β· 8 min Β· 3 hands-on steps

Numbers & Math

Use JavaScript as a calculator, including the remainder operator programmers love.

JavaScript is a calculator

Operator Meaning Example Result
+ add 5 + 2 7
- subtract 5 - 2 3
* multiply 5 * 2 10
/ divide 5 / 2 2.5
% remainder 5 % 2 1
** power 5 ** 2 25

Try them all. Change the numbers and run again:

JavaScript Β· try it
Loading…

Order of operations

Just like school math: parentheses first, then **, then * / %, then + -.

JavaScript Β· try it
Loading…

When in doubt, add parentheses. They make your intention obvious to the next reader too.

The remainder operator %

% looks strange, but you'll use it a lot:

JavaScript Β· try it
Loading…

Decimals can surprise you

JavaScript Β· try it
Loading…

That's not a bug in JavaScript. Computers store decimals in binary, and some numbers can't be stored exactly (like 1/3 in decimal). For money, developers often count in cents (whole numbers) instead.

πŸ§ͺ Practice: 3 steps

0 / 3 done

Step 1 of 3 Β· Minutes in a week

Print how many minutes are in a week. Let the computer do the math: 7 days Γ— 24 hours Γ— 60 minutes.

JavaScript
Loading editor…

Output appears here. Press Run or Ctrl/⌘ + Enter.

βœ… Check your understanding

  1. What is 10 % 4?

  2. What does 2 + 3 * 4 equal in JavaScript?

Create a free account to track your progress.