All lessons

Numbers & Math

6 min · JavaScript

Use JavaScript as a calculator, and do math with your variables.

JavaScript · Numbers & Math0/5 cleared

Enemy 1 of 5

This code multiplies price by quantity and prints the answer, not the expression. Numbers without quotes are treated as math. Press Run to see 15.

Why this works

Print a math expression and JavaScript evaluates it first (works out the numeric result) before console.log ever sees it, so only the final answer gets printed, never the expression itself.

The basic operators are + (add), - (subtract), * (multiply), and / (divide), and JavaScript follows the standard order of operations: multiplication and division happen before addition and subtraction, unless you use parentheses to change that order. So 2 + 3 * 4 evaluates to 14, not 20.

Numbers don't need quotes: that's the key signal JavaScript uses to know you mean math rather than text. Quoting a number, like "5", turns it into a string, and using + on a string and a number together doesn't add them numerically. It concatenates them as text instead ("5" + 3 gives "53", not 8). This quirk trips up a lot of beginners, so it's worth remembering early.

You
Syntax Skeleton
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.