Simple 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:
12345let text = "Hello World!" let number = 12345 let boolean = true console.log(`First variable: ${text}, second variable: ${number}, third: ${boolean}`)
As you can see, the syntax for declaring a variable looks like this:
javascriptlet variable_name = value
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
andconst
instead ofvar
. For now, the examples will still includevar
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:
12let sum = 5 + 3; console.log(sum)
Subtraction (
-
): Subtracts one number from another:
12let difference = 10 - 4; console.log(difference)
Multiplication (
*
): Multiplies two numbers:
12let product = 6 * 7; console.log(product)
Division (
/
): Divides one number by another:
12let quotient = 20 / 4; console.log(quotient)
Modulus (
%
): Returns the remainder of the division:
12let remainder = 15 % 4; console.log(remainder)
Exponentiation (
**
orMath.pow()
):** Raises a number to a power:
12let power = 2 ** 3; console.log(power)
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?
Tak for dine kommentarer!
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat