Numbers & Math
6 min · JavaScript
Use JavaScript as a calculator — and do math with your variables.
Enemy 1 of 5
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.
Loading editor...