Start learning free

LESSON 17 OF 348 · CALCULATIONS WITH NUMBERS

Arithmetic Operators in TypeScript: + - * / % and **

Lesson Read the full explanation

TL ; DR:

  • We can tell computers to perform calculations for us using +, -, *, /:

    console.log(2 + 6); // Prints 8
    console.log(10 - 7); // Prints 3
    console.log(3 * 3); // Prints 9
    console.log(6 / 2); // Prints 3
    


  • We can also use variables. In these examples, TypeScript will replace variable num with its value 6 to do the calculations:

    let num = 6;
    console.log(num + 5); // Prints 11
    console.log(10 - num); // Prints 4
    console.log(3 * num); // Prints 18
    console.log(num / 2); // Prints 3
    



Full lesson:

Computers absolutely excel at performing calculations. The "compute" in their name comes from their historical association with providing answers to mathematical questions.

These calculations can be performed either directly with numbers or with variables that are assigned numeric values.


Arithmetic operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Examples
+ Addition 3 + 5,  x + 5,  3 + y,  x + y
- Subtraction 8 - 3,  8 - y,  x - 3,  x - y
* Multiplication 3 * 5,  3 * y,  x * 5,  x * y
/ Division 8 / 4,  8 / y,  x / 8,  x / y
% Modulus 8 % 4,   8 % y,  x % 4,  x % y
** Exponentiation 3 ** 2,  3 ** y,   x ** 3,  x ** y


Note that performing arithmetic on variables does not change the variable - you can only update a variable using the = sign.

Let's go through some examples together to better understand how each operator works:


Addition

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(a + b); // Output: 5

console.log(a + 10); // Output: 12

Subtraction

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(a - b); // Output: -1

console.log(10 - b); // Output: 7

Multiplication

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(a * 5); // Output: 10

console.log(-2 * a * b); // Output: -12

Division

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(b / a); // Output: 1.5

console.log(a / 10); // Output: 0.2

Exponentiation

The ** operator performs the exponential calculation (2**3 translates to 2 * 2 * 2):

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(2 ** 3); // Output: 8

console.log(b ** a); // Output: 9

We left the modulo operator (%) for the next lesson.


Assignment
Follow the Coding Tutorial and let's practice with arithmetic operators!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Arithmetic Operators in TypeScript, explained

    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:

    OperatorNameExampleResult
    +Addition7 + 29
    -Subtraction7 - 25
    *Multiplication7 * 214
    /Division7 / 23.5
    %Remainder7 % 21
    **Exponentiation7 ** 249

    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 / 2 is 3.5, not 3. TypeScript has one number type 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.2 prints 0.30000000000000004. This is normal, and it is not a TypeScript bug.
    • Dividing by zero doesn't crash. 1 / 0 is Infinity and 0 / 0 is NaN, a special "not a number" value.
    • The operands must be numbers. The compiler reports arithmetic like "5" - 1 as an error about the left-hand side of an arithmetic operation. With a string variable s holding "5", the JavaScript that is still produced makes s - 1 equal 4, but you should convert with Number instead of relying on that. The one exception is +, which joins text: "5" + 1 is the string "51".
    • No integer overflow. A number is a 64-bit double, so very large results lose precision rather than wrapping. Whole numbers are exact only up to 2 ** 53 (the constant Number.MAX_SAFE_INTEGER is 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.

    1. The products. Four items at the same price is a multiplication: 4 * price.
    2. The bag. One extra cost is added to that: 4 * price + 15.
    3. 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 on number values, whether literals or variables.
    • An expression doesn't change the variables inside it. Assign the result to keep it.
    • / keeps the fractional part: 7 / 2 is 3.5.
    • Decimal results can carry tiny rounding errors.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free