This page looks best with JavaScript enabled

TypeScript's Variable Wars: Battle of let vs. const vs. var – Which One Wins?

 ·  ☕ 5 min read

In the vast world of TypeScript, variable declarations are the building blocks of every program. They enable us to store and manipulate data, providing flexibility and control over our code. However, TypeScript offers three distinct variable declarations: let, const, and var. Each declaration has its own characteristics and use cases. In this professional blog post, we will delve deep into the differences between let, const, and var, accompanied by definitive examples, to understand their unique features and make informed decisions in our TypeScript projects.

1. var: The Classic, but Risky, Choice

The var declaration has been present in JavaScript since its inception, and it continues to be supported in TypeScript for compatibility reasons. However, it comes with certain caveats and pitfalls that have led to its diminished usage in modern TypeScript projects.

One key characteristic of var is its function or global scope. Variables declared with var are accessible throughout the function or globally if declared outside any function. This can lead to unintended variable hoisting, where variable declarations are moved to the top of their scope during compilation. Let’s illustrate this with an example:

1
2
3
4
5
function varExample() {
  console.log(message); // Output: undefined
  var message = "Hello, TypeScript!";
  console.log(message); // Output: Hello, TypeScript!
}

In the above code, the variable message is hoisted, resulting in the first console.log() statement printing undefined. The declaration is moved to the top, but the assignment remains in place. This behavior can introduce confusion and subtle bugs, making code maintenance challenging.

Another drawback of var is its lack of block scoping. Variables declared with var are not confined to the block ({}) in which they are defined. This can lead to unintended redeclarations and overwriting of variables within the same scope, as demonstrated by the following example:

1
2
3
4
5
6
7
8
function varRedeclarationExample() {
  var count = 5;
  if (true) {
    var count = 10;
    console.log(count); // Output: 10
  }
  console.log(count); // Output: 10
}

In this case, both variables named count refer to the same value, as the second declaration overwrites the first. This can introduce subtle bugs and hinder code maintainability.

2. let: Embracing Block Scoping and Enhanced Readability

With the introduction of ECMAScript 2015 (ES6), the let declaration brought a significant improvement to variable scoping in JavaScript and TypeScript. let provides block scoping, meaning variables are confined to the block ({}) in which they are defined.

Block scoping enhances code readability and reduces the risk of unintended variable redeclarations or overwriting. Consider the following example:

1
2
3
4
5
6
7
8
function letExample() {
  let message = "Hello, TypeScript!";
  if (true) {
    let message = "Greetings!";
    console.log(message); // Output: Greetings!
  }
  console.log(message); // Output: Hello, TypeScript!
}

In this case, the message variable within the inner block is distinct from the outer block, preserving their respective values. This promotes code clarity and reduces the chances of bugs caused by variable collisions.

Additionally, let does not exhibit hoisting, ensuring that variables must be declared before they are accessed. This enhances code predictability and reduces potential issues. Consider the following example:

1
2
3
4
5
function letHoistingExample() {
  console.log(message); // Output: ReferenceError: message is not defined
  let message = "Hello, TypeScript!";
  console.log(message); // Output: Hello, TypeScript!
}

In this case, an attempt to access the message variable before its declaration results in a ReferenceError, clearly indicating that variables declared with let must be defined before they are used.

3. const: Immutable Constants for Robustness

The const declaration in TypeScript serves a specific purpose: defining immutable variables that hold constant values. Once assigned, the value of a const variable cannot be reassigned or mutated.

Using const provides guarantees of immutability, making the code more robust and preventing accidental modifications. Consider the following example:

function constExample() {
  const PI = 3.14159;
  console.log(PI); // Output: 3.14159
}

In this case, the const variable PI holds a constant value, and any attempt to modify its value will result in an error. This ensures the integrity of critical values and protects against accidental changes.

Similar to let, const also supports block scoping. Constants declared with const are confined to their respective blocks, ensuring encapsulation and preventing unwanted access or modifications.

1
2
3
4
5
6
7
function constBlockScopeExample() {
  if (true) {
    const message = "Hello, TypeScript!";
    console.log(message); // Output: Hello, TypeScript!
  }
  console.log(message); // Output: ReferenceError: message is not defined
}

In this example, the message constant is only accessible within the inner block, and any attempt to access it outside that scope will result in a ReferenceError. This promotes code clarity and prevents unintended modifications.

Understanding the differences between let, const, and var is crucial for writing reliable and maintainable TypeScript code. While var may still be used for legacy code or specific scenarios, let and const offer superior scoping, readability, and immutability.

By embracing let and const and minimizing the usage of var, developers can enhance code predictability, reduce bugs caused by scoping issues, and ensure the integrity of critical values. So, make an informed choice when declaring variables in your TypeScript projects, and let let and const empower you to write cleaner, more robust code.

Remember, the right choice of variable declaration can make a significant impact on the quality and maintainability of your codebase. Happy coding!

Thank you for taking the time to read this content! If you found it helpful and would like to receive more informative articles, tips, and insights, I encourage you to follow me. By following, you’ll stay updated with the latest content and ensure that you don’t miss out on any valuable information. I appreciate your support and look forward to sharing more knowledge with you in the future.

Share on
Support the author with

Dinesh Arora
WRITTEN BY
Dinesh Arora
Developer