Course Content
Introduction to TypeScript
Introduction to TypeScript
Mathematics in TypeScript
Have you also heard that you don't need math for programming? I'm sorry to disappoint you, but you do. However, it's just the basics! In this chapter, we will explore how to use your arithmetic knowledge in TypeScript programming.
Let's start with what you already know. We can perform operations on numbers using the tools we discussed in the previous chapter. Let's take a look at some example code:
console.log(150 + 150); console.log(900 / 3);
This is the simplest example of using mathematical operations in TypeScript. However, you may have seen this in the previous chapter, so let's look at a more complex example where we use multiple operations:
let res = 20 * 10 - 75 / (22 + 3) - 2 ** 4; console.log(res);
It's important to understand the order of execution of mathematical operations. From your school days, you may remember that operations inside parentheses come first, followed by exponentiation, and so on. Let's break down the expression above to refresh these concepts:
Each mathematical expression can be broken down into a queue of subtasks. From the video above, it's evident that operations inside parentheses are executed first, followed by exponentiation, multiplication/division, and only then addition and subtraction. Just some simple math.
Interaction of Numbers and Variables
I hope the order of execution of mathematical operations is clear now. Let's now look at how we can combine variables and numbers:
let number_1 = 10; let number_2 = 15; console.log(number_1 + number_2);
We can perform mathematical operations on two variables with numeric types. However, if one of the variables has a different type, the operation will give us some weird result:
let num : number = 20; let str : number = '23'; console.log(num + str);
As you can see in the example above, the mathematical operation did not execute. Instead, we performed concatenation. This is a term that describes the addition of strings to each other. But let's not jump to conclusions; let's try to perform another mathematical operation with the same variables:
let num = 20; let str = '10'; console.log(num - str); console.log(num / str); console.log(num ** str)
Yes, we can use mathematical operations (except addition) on different data types. Yes, this is why everyone is excited about JavaScript and TypeScript. No, I cannot explain why this happens. You just need to accept it as a fact.
Note
The TypeScript compiler will produce errors, but it will still consider such expressions. This happens because TypeScript is transpiled into JavaScript after the code is executed.
Can mathematical operations be used between a variable and a number?
Yes.
let num = 30; console.log(num - 10);
Note
Unlike JavaScript, the TypeScript compiler highlights an error when we try to subtract a string from a number. This code will execute, but we will be warned that we are doing something wrong.
Thanks for your feedback!