Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

TypeScript Variables: Let, Const, and Var Explained

Posted on April 4, 2025 • 6 min read • 1,197 words
Share via
Mapagam
Link copied to clipboard

Learn the differences between TypeScript variables: let, const, and var. Understand scoping, reassignment, and best practices for each.

On this page
1. Overview of Variable Declarations in TypeScript 2. The let Keyword 2.1. Introduction to let 2.2. Scoping with let 2.3. Reassigning let Variables 2.4. Avoiding Common Mistakes with let 3. The const Keyword 3.1. Introduction to const 3.2. Scoping with const 3.3. Immutability of const 3.4. Best Practices with const 4. The var Keyword 4.1. Introduction to var 4.2. Scoping with var 4.3. Hoisting with var 4.4. Why Avoid var 5. Differences Between let, const, and var 6. When to Use let, const, and var 6.1. Use let When: 6.2. Use const When: 6.3. Avoid Using var When: 7. Conclusion

In modern web development, TypeScript has emerged as one of the most popular languages due to its ability to enhance JavaScript by adding static types. One of the fundamental aspects of any programming language is variable declaration. In JavaScript (and by extension, TypeScript), variables are declared using three keywords: let, const, and var. Understanding the differences between these variable declaration methods is crucial for writing clean, maintainable, and efficient code.

1. Overview of Variable Declarations in TypeScript

Before we explore each declaration keyword individually, it’s essential to understand the concept of variable declarations. Variables are used to store data that can be referenced and manipulated later in the program. In TypeScript, as in JavaScript, there are three primary ways to declare a variable:

  1. let: Introduced in ES6, let allows you to declare variables that can be reassigned.
  2. const: Also introduced in ES6, const is used to declare variables whose values cannot be reassigned.
  3. var: The older of the three, var was the primary way to declare variables in JavaScript before ES6. It has scoping rules that are quite different from let and const.

Each of these keywords has unique scoping, hoisting, and reassignment behaviors that make them appropriate in different scenarios. Let’s take a closer look at each.

2. The let Keyword

2.1. Introduction to let

In TypeScript, the let keyword is used to declare a variable that can be reassigned later. Unlike var, let provides block-level scoping. This means that the variable is only accessible within the block, statement, or expression where it was declared.

2.2. Scoping with let

One of the key differences between let and var is scoping. Variables declared with let are scoped to the block, meaning that they are accessible only within the nearest enclosing block (such as a loop, condition, or function).

Example:

if (true) {
  let x = 10;
  console.log(x); // 10
}
console.log(x); // Error: x is not defined

In the above code, the variable x is only accessible within the if block. Attempting to access x outside this block will result in an error.

2.3. Reassigning let Variables

Variables declared with let can be reassigned new values during the course of the program.

Example:

let age = 25;
age = 30; // Reassignment is allowed
console.log(age); // 30

Here, the variable age is initially set to 25 and later reassigned to 30.

2.4. Avoiding Common Mistakes with let

Although let makes scoping clearer, it still presents some challenges if not used correctly. Developers should avoid excessive use of let for variables that should never be reassigned, as this can lead to unnecessary mutability.

3. The const Keyword

3.1. Introduction to const

The const keyword is used in TypeScript to declare a variable whose value cannot be reassigned. This does not mean that the value of the variable is immutable; instead, it only ensures that the variable cannot be reassigned a different value. const also provides block-level scoping, similar to let.

3.2. Scoping with const

Like let, variables declared with const are also scoped to the block in which they are declared.

Example:

if (true) {
  const userName = "Alice";
  console.log(userName); // Alice
}
console.log(userName); // Error: userName is not defined

Here, the userName variable is confined to the if block and is inaccessible outside it.

3.3. Immutability of const

It’s important to note that const only prevents reassignment of the variable, not mutation of the value it holds. If the variable holds an object or an array, the contents of the object or array can still be changed, but the reference to the object or array cannot be reassigned.

Example:

const person = { name: "Bob", age: 28 };
person.age = 29; // This is allowed: Mutating the object
person = { name: "Alice", age: 30 }; // Error: Assignment to constant variable

In the above example, person can still have its properties mutated (i.e., age can be changed), but the entire object cannot be reassigned to a new object.

3.4. Best Practices with const

Use const when you know the variable should never be reassigned. This helps in creating cleaner code that is easier to reason about and debug. const should be the default choice for most variables, especially when working with functions, configuration settings, and constants.

4. The var Keyword

4.1. Introduction to var

var is the oldest variable declaration keyword in JavaScript and TypeScript. It was used prior to ES6 and is still supported for backward compatibility. However, its behavior can be confusing, which is why many developers avoid using var in favor of let and const.

4.2. Scoping with var

The key difference between var and let or const is its scoping. var is function-scoped, meaning that it is only accessible within the function where it is declared. If declared outside any function, var is globally scoped.

Example:

function testVar() {
  if (true) {
    var x = 10;
  }
  console.log(x); // 10
}
testVar();

Here, even though x was declared within the if block, it is still accessible outside of it, because var is function-scoped, not block-scoped.

4.3. Hoisting with var

Another major difference with var is hoisting. Variables declared with var are hoisted to the top of their scope. This means that the declaration is moved to the top during execution, but the initialization of the variable happens at the line where it is written.

Example:

console.log(y); // undefined
var y = 5;
console.log(y); // 5

In the above example, y is hoisted to the top, but its value is undefined until the code execution reaches the assignment.

4.4. Why Avoid var

Due to the issues with scoping and hoisting, var can lead to unexpected behavior, especially in larger codebases. Using let and const is generally recommended to avoid these problems.

5. Differences Between let, const, and var

Feature let const var
Scoping Block-scoped Block-scoped Function-scoped
Reassignable Yes No Yes
Hoisting Hoisted (initialized as undefined) Hoisted (initialized as undefined) Hoisted (initialized as undefined)
Best Usage For variables that may change For constants or immutable data Avoid unless backward compatibility required

6. When to Use let, const, and var

6.1. Use let When:

  • The value of the variable will change over time.
  • You need block-level scoping for clarity and maintainability.
  • You need to declare a loop variable or temporary value.

6.2. Use const When:

  • The value should not change once set.
  • You are working with references (like objects or arrays) but want to avoid reassignment of the reference itself.

6.3. Avoid Using var When:

  • You want predictable scoping and don’t need function-scoped variables.
  • You want to avoid issues related to hoisting.
  • You’re working in a modern TypeScript/JavaScript environment and need cleaner code.

7. Conclusion

Understanding the differences between let, const, and var is crucial for writing efficient and bug-free TypeScript code. While let and const are the preferred choices in modern development due to their block-level scoping and predictable behaviors, var should be avoided to prevent scoping issues. By using const for immutable values and let for variables that change, developers can create cleaner, more maintainable code.

As TypeScript continues to gain popularity, a deep understanding of these fundamental concepts will ensure that your code is robust, easier to debug, and ready for long-term success.

TypeScript   Let vs Const vs Var   TypeScript Variables   JavaScript Variables   TypeScript Best Practices  
TypeScript   Let vs Const vs Var   TypeScript Variables   JavaScript Variables   TypeScript Best Practices  
 Mastering TypeScript Functions: The Ultimate Guide
Understanding TypeScript Data Types with Examples 

More Reading!

  1. Understanding TypeScript Decorators: A Step-by-Step Guide
  2. How TypeScript’s Type Inference Works (And Why It’s a Game-Changer)
  3. TypeScript Type Assertions: When and How to Use Them Effectively
  4. How JavaScript Variables Actually Work: var vs let vs const
  5. TypeScript Tuples: What They Are and How to Use Them
On this page:
1. Overview of Variable Declarations in TypeScript 2. The let Keyword 2.1. Introduction to let 2.2. Scoping with let 2.3. Reassigning let Variables 2.4. Avoiding Common Mistakes with let 3. The const Keyword 3.1. Introduction to const 3.2. Scoping with const 3.3. Immutability of const 3.4. Best Practices with const 4. The var Keyword 4.1. Introduction to var 4.2. Scoping with var 4.3. Hoisting with var 4.4. Why Avoid var 5. Differences Between let, const, and var 6. When to Use let, const, and var 6.1. Use let When: 6.2. Use const When: 6.3. Avoid Using var When: 7. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard