Updated July 5, 2023
Introduction to TypeScript let
In TypeScript, the declaration of variables can be done using the keywords var, const, and let. However, using the keyword var can lead to certain problems. To overcome these problems, the keyword let is utilized for variable declaration in TypeScript. The syntax for declaring variables using let is the same as that of var. One important difference is that variables declared using let have a block scope.
The syntax to declare variables using let in TypeScript is as follows:
let variable_name;
where variable_name is the name of the variable declared using the let keyword.
Working of let keyword to declare variables in TypeScript is as follows:
- There are several drawbacks while dealing with the variables declared using the var keyword.
- In order to overcome the drawbacks caused by declaring the variables using the var keyword, we use the let keyword to declare the variables.
- The variables declared using the let keyword have a block scope.
- The variables declared using the let keyword cannot be accessed or modified outside the block in which they are declared.
Examples of TypeScript let
Let us discuss examples.
Example #1
TypeScript program to declare a variable using the let keyword and to declare another variable using the var keyword and demonstrate the difference of scope between the two variables:
Code:
//declaring a variable using var keyword
var globvar = 2;
//defining a function within which two variables num and num1 are declared using let keyword
function power()
{
let num = 10;
let num1 = Math.pow(num,globvar);
//defining a if condition statement within which a variable num2 is declared using let keyword
if(num1 >= 100)
{
let num2 = Math.sqrt(num1);
}
//displaying the value of the variables declared using var and let keywords within the scope of function
console.log("The value of the variable declared using var keyword is:\n",globvar);
console.log("The value of the variable num declared using let keyword inside the scope of function block is:\n",num);
console.log("The value of the variable num1 declared using let keyword inside the scope of function block is:\n",num1);
console.log("The value of the variable num2 declared using let keyword inside the scope of if block is:\n",num2);
}
power();
//displaying the value of the variables declared using var and let keywords outside the scope of function
console.log("The value of the variable declared using var keyword is:\n",globvar);
console.log("The value of the variable num declared using let keyword inside the scope of function block is:\n",num);
console.log("The value of the variable num1 declared using let keyword inside the scope of function block is:\n",num1);
console.log("The value of the variable num2 declared using let keyword inside the scope of if block is:\n",num2);
The output of the above program is as shown in the snapshot below:
The errors produced for the above program is as shown in the snapshot below:
In the above program, we are declaring a variable called globvar using the var keyword. Then we are declaring two other variables called num and num1 within the function using the let keyword. Then within the if condition statement inside the function, we are defining another variable called num2 using the let keyword. Then, we display the values of variables declared within the scope of the function.
Example #2
TypeScript program to declare a variable using the let keyword and to declare another variable using the var keyword and demonstrate the difference of scope between the two variables:
Code:
//declaring a variable using var keyword
var globvar = 3;
//defining a function within which two variables num and num1 are declared using let keyword
function power()
{
let num = 2;
let num1 = Math.pow(num,globvar);
//defining a if condition statement within which a variable num2 is declared using let keyword
if(num1 >= 8)
{
let num2 = Math.sqrt(num1);
}
//displaying the value of the variables declared using var and let keywords within the scope of function
console.log("The value of the variable declared using var keyword is:\n",globvar);
console.log("The value of the variable num declared using let keyword inside the scope of function block is:\n",num);
console.log("The value of the variable num1 declared using let keyword inside the scope of function block is:\n",num1);
console.log("The value of the variable num2 declared using let keyword inside the scope of if block is:\n",num2);
}
power();
//displaying the value of the variables declared using var and let keywords outside the scope of function
console.log("The value of the variable declared using var keyword is:\n",globvar);
console.log("The value of the variable num declared using let keyword inside the scope of function block is:\n",num);
console.log("The value of the variable num1 declared using let keyword inside the scope of function block is:\n",num1);
console.log("The value of the variable num2 declared using let keyword inside the scope of if block is:\n",num2);
The output of the above program is as shown in the snapshot below:
The errors produced for the above program is as shown in the snapshot below:
In the above program, we are declaring a variable called globvar using the var keyword. Then within the if condition statement inside the function, we are defining another variable called num2 using the let keyword. This includes the values of variables such as globvar, num, and num1.
Conclusion
In this article, we have learned the concept of the let keyword in TypeScript through the definition, syntax, and declaration of variables using the let keyword through programming examples and their outputs.
Recommended Articles
We hope that this EDUCBA information on “TypeScript let” was beneficial to you. You can view EDUCBA’s recommended articles for more information.