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

Course Content

Introduction to TypeScript

Mathematics in TypeScriptMathematics 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:

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:

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:

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:

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:

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.

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?

Select the correct answer

2 + 2 * 2 = ?

Select the correct answer

Everything was clear?

Section 1. Chapter 4
course content

Course Content

Introduction to TypeScript

Mathematics in TypeScriptMathematics 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:

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:

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:

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:

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:

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.

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?

Select the correct answer

2 + 2 * 2 = ?

Select the correct answer

Everything was clear?

Section 1. Chapter 4
some-alt