Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Mathematics in TypeScript | TypeScript Fundamentals
Introduction to TypeScript
course content

Зміст курсу

Introduction to TypeScript

Introduction to TypeScript

1. TypeScript Fundamentals
2. Conditional Statements
3. Arrays
4. Loops
5. Functions

bookMathematics 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:

12
console.log(150 + 150); console.log(900 / 3);
copy

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:

12
let res = 20 * 10 - 75 / (22 + 3) - 2 ** 4; console.log(res);
copy

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:

123
let number_1 = 10; let number_2 = 15; console.log(number_1 + number_2);
copy

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:

123
let num : number = 20; let str : number = '23'; console.log(num + str);
copy

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:

12345
let num = 20; let str = '10'; console.log(num - str); console.log(num / str); console.log(num ** str)
copy

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.

12
let num = 30; console.log(num - 10);
copy

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.

1. What will be the result of the below code?
2. 2 + 2 * 2 = ?
What will be the result of the below code?

What will be the result of the below code?

Виберіть правильну відповідь

2 + 2 * 2 = ?

2 + 2 * 2 = ?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt