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

Course Content

Introduction to TypeScript

Introduction to TypeScript

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

bookSimple variable declaration

Variables greatly simplify the lives of all developers. You can store text, numbers, or a boolean value (true or false) in a variable. Let's look at the syntax for creating a variable and displaying it in the console:

12345
let text = "Hello World!" let number = 12345 let boolean = true console.log(`First variable: ${text}, second variable: ${number}, third: ${boolean}`)
copy

As you can see, the syntax for declaring a variable looks like this:

Also, note that when we use the let keyword to declare a variable, both TypeScript and JavaScript automatically determine what type this variable should be. TypeScript allows you to specify the data type of a variable (that's why it's called TypeScript), which I will tell you about later in this course. For now, let's focus on simple variable declarations.

Note

Remember that we use let and const instead of var. For now, the examples will still include var because it's the older way of declaring variables in TypeScript and JavaScript that has been around for a long time. Later on, we will explore the newer ways of declaring variables.

Arithmetic Operations in TypeScript

TypeScript, like any other programming language, supports simple and complex mathematical operations. Let's take a look at basic operations:

  • Addition (+): Adds two numbers:
12
let sum = 5 + 3; console.log(sum)
copy
  • Subtraction (-): Subtracts one number from another:
12
let difference = 10 - 4; console.log(difference)
copy
  • Multiplication (*): Multiplies two numbers:
12
let product = 6 * 7; console.log(product)
copy
  • Division (/): Divides one number by another:
12
let quotient = 20 / 4; console.log(quotient)
copy
  • Modulus (%): Returns the remainder of the division:
12
let remainder = 15 % 4; console.log(remainder)
copy
  • Exponentiation ( ** or Math.pow()):** Raises a number to a power:
12
let power = 2 ** 3; console.log(power)
copy

Phew, I hope, I've listed everything. This is a list of the most commonly used arithmetic operations in TypeScript. I'll tell you how to use these operations in the next chapter, where you'll learn how to use them correctly and what you can achieve with their help.

1. What will be the output of the following code?
2. How to declare a variable in TypeScript?
What will be the output of the following code?

What will be the output of the following code?

Select the correct answer

How to declare a variable in TypeScript?

How to declare a variable in TypeScript?

Select a few correct answers

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt