What arithmetic operators do
Arithmetic operators perform calculations on numbers. You can apply them to literal numbers, to variables that hold numbers, or to a mix of both. TypeScript has six of them:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3.5 |
% | Remainder | 7 % 2 | 1 |
** | Exponentiation | 7 ** 2 | 49 |
The lesson focuses on the first four and on **. The remainder operator % gets its own lesson, so here it appears only in the table.
Using them with variables
When an operator meets a variable, TypeScript replaces the variable with its current value. All numbers have the same type, number, so you can mix them freely:
let a: number = 2;
let b: number = 3;
console.log(a + b); // 5
console.log(a - b); // -1
console.log(a * 5); // 10
console.log(-2 * a * b);// -12
console.log(b / a); // 1.5
console.log(a / 10); // 0.2
console.log(b ** a); // 9
Using a variable in an expression doesn't change it. Only an assignment with = does:
let num: number = 6;
num + 5;
console.log(num); // 6
num = num + 5;
console.log(num); // 11
The line num + 5; computes 11 and throws it away. If you want to keep a result, store it in a variable or pass it straight to console.log.
Exponentiation
The ** operator raises the left number to the power of the right one, so 2 ** 3 means 2 * 2 * 2:
console.log(2 ** 3); // 8
console.log(2 ** 10); // 1024
console.log(9 ** 0.5); // 3
Two details are worth remembering. A chain of powers is evaluated from right to left, so 2 ** 3 ** 2 is 2 ** 9, which is 512, and (2 ** 3) ** 2 is 64. And a leading minus sign can't be put directly in front of the base: -2 ** 2 is a syntax error, so you must write (-2) ** 2 or -(2 ** 2) to say which one you mean.
Things that surprise beginners
- Division gives decimals.
7 / 2is3.5, not 3. TypeScript has onenumbertype for whole and fractional values, so there is no separate integer division. A later lesson shows how to get the whole part. - Decimals are approximate. Numbers are stored in binary floating point, so
0.1 + 0.2prints0.30000000000000004. This is normal, and it is not a TypeScript bug. - Dividing by zero doesn't crash.
1 / 0isInfinityand0 / 0isNaN, a special "not a number" value. - The operands must be numbers. The compiler reports arithmetic like
"5" - 1as an error about the left-hand side of an arithmetic operation. With a string variablesholding"5", the JavaScript that is still produced makess - 1equal 4, but you should convert withNumberinstead of relying on that. The one exception is+, which joins text:"5" + 1is the string"51". - No integer overflow. A
numberis a 64-bit double, so very large results lose precision rather than wrapping. Whole numbers are exact only up to 2 ** 53 (the constantNumber.MAX_SAFE_INTEGERis 2 ** 53 - 1).
The exercise, step by step
The starter code gives the price of one product:
let price: number = 83;
Andy buys 4 products at price dollars each, plus a bag that costs 15 dollars. The task is to print his total.
- The products. Four items at the same price is a multiplication:
4 * price. - The bag. One extra cost is added to that:
4 * price + 15. - Printing. Pass the whole expression to
console.log. There is no need to store it first.
Multiplication is evaluated before addition, as in school arithmetic, so the expression needs no parentheses. The next lessons cover this rule in detail.
let price: number = 83;
console.log(4 * price + 15);
It prints 347, since 4 times 83 is 332 and 332 plus 15 is 347.
A common slip is to write 4 * (price + 15). That would mean Andy pays for 15 dollars of bag four times, giving 392 instead of 347. The bag is bought once, so only the products are multiplied. Reading the expression aloud, "four times the price, plus fifteen", helps you check that it matches the story in the task.
Key takeaways
+,-,*,/and**work onnumbervalues, whether literals or variables.- An expression doesn't change the variables inside it. Assign the result to keep it.
/keeps the fractional part:7 / 2is3.5.- Decimal results can carry tiny rounding errors.