Start learning free

LESSON 19 OF 348 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in TypeScript: Combining Operators

Lesson Read the full explanation

  • We can perform multiple calculations using operators in the same line of code:

    console.log(3 - 4 + 6); // Prints 5
    console.log(5 + 2 - 3); // Prints 4
    console.log(2 * 5 + 1); // Prints 11
    console.log(4 / 2 * 5); // Prints 10
    

  • We can also use variables:

    let num1 = 5;
    let num2 = -1;
    
    console.log(2 * num1 * num2); // Prints -10
    console.log(num1 + num2 - 3); // Prints 1
    console.log(20 / num1 + num2); // Prints 3
    console.log(num1 / 5 * num2); // Prints -1
    



Assignment
Follow the Coding Tutorial and let's practice with mathematical expressions!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Mathematical Expressions in TypeScript, explained

    Several operations in one expression

    An expression is a piece of code that produces a value. You can chain several operators in one expression, with numbers, variables or both. TypeScript works out the whole expression and gives you a single number.

    console.log(3 - 4 + 6);   // 5
    console.log(5 + 2 - 3);   // 4
    console.log(2 * 5 + 1);   // 11
    console.log(4 / 2 * 5);   // 10
    

    Variables work the same way. TypeScript replaces each name with its current value before it calculates:

    let num1: number = 5;
    let num2: number = -1;
    
    console.log(2 * num1 * num2);   // -10
    console.log(num1 + num2 - 3);   // 1
    console.log(20 / num1 + num2);  // 3
    console.log(num1 / 5 * num2);   // -1
    

    The order of evaluation

    Two rules decide what happens first. You will study them fully in the next lessons, but they already matter here:

    1. Multiplication and division come before addition and subtraction. In 2 * 5 + 1 the product 10 is computed first, then 1 is added.
    2. Operators of the same level go from left to right. In 3 - 4 + 6 the subtraction happens first, giving -1, and then 6 is added. In 4 / 2 * 5 the division happens first, giving 2, and then the multiplication gives 10.

    The second rule is the one that causes mistakes. If you read 10 - 4 - 3 as "10 minus the result of 4 minus 3", you would get 9, but TypeScript computes (10 - 4) - 3, which is 3. The same is true for division: 8 / 4 / 2 is 1, while 8 / (4 / 2) is 4. When the order is not obvious to a reader, add parentheses even if the language doesn't need them.

    Translating a story into an expression

    Word problems become expressions when you name each quantity and decide how it combines. Take the exercise. The starter code has two variables:

    let price: number = 93;
    let bag: number = 7;
    

    Here price is the cost of one product and bag is the cost of one bag.

    Step 1: Andy buys four products and one bag

    Four products cost 4 * price. The bag is added once. Because multiplication comes first, 4 * price + bag already means "four prices, then add the bag", and it needs no parentheses. Passing it to console.log prints the total, 379, since 4 times 93 is 372 and 372 plus 7 is 379.

    Step 2: Mary has 150 dollars and wants two products and one bag

    Her total is 2 * price + bag, which is 193. She already has 150, so the amount she still needs is the total minus 150. Written in one expression, that is 2 * price + bag - 150. The operators + and - are on the same level, so they run left to right: first the sum 193 is built, and only then is 150 taken away. The result, 43, is what Mary needs to add.

    let price: number = 93;
    let bag: number = 7;
    
    console.log(4 * price + bag);
    console.log(2 * price + bag - 150);
    

    The program prints:

    379
    43
    

    A good habit is to check an expression by hand with other numbers. If price were 10 and bag were 5, the first line should print 45 and the second should print -125, a negative number meaning Mary would have 125 dollars left over. If your result doesn't match your hand calculation, the structure of the expression is wrong, not the arithmetic.

    Mistakes to avoid

    • Adding parentheses that change the meaning. 4 * (price + bag) buys four bags as well, and gives 400 instead of 379.
    • Subtracting from the wrong part. 2 * (price + bag - 150) subtracts Mary's money before doubling, and prints -100. The money is subtracted once from the whole total, so keep the structure "total, then subtract what she has".
    • Hard-coding the numbers. Write the names price and bag, not 93 and 7, so the expression follows the variables.
    • Expecting whole numbers from division. / gives a fraction when the result isn't whole: 7 / 2 is 3.5. Decimal results can also carry tiny rounding errors, so 0.1 + 0.2 is 0.30000000000000004.

    Key takeaways

    • Many operators can share an expression, and the whole thing becomes one value.
    • * and / go before + and -; equal-level operators go left to right.
    • Pass a complete expression to console.log to print its result.
    • Build expressions from the story one piece at a time, then read them aloud to check them.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free