EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript let
 

TypeScript let

Shobha Shivakumar
Article byShobha Shivakumar
EDUCBA
Reviewed byRavi Rathore

Updated July 5, 2023

TypeScript let

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

TypeScript let 1

The errors produced for the above program is as shown in the snapshot below:

Output 2

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:

Output 3

The errors produced for the above program is as shown in the snapshot below:

Output 4

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.

  1. TypeScript Versions
  2. TypeScript Array
  3. Typescript Interface vs Class
  4. TypeScript Types

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW