TypeScript const

Introduction

In this chapter, we will learn about the const keyword in TypeScript. The const keyword is used to declare variables that are block-scoped and cannot be reassigned after their initial assignment. Understanding how to use const helps ensure that values intended to remain constant are not accidentally modified, promoting code stability and readability.

Table of Contents

  1. Definition
  2. Syntax
  3. Block Scope
  4. Reassignment
  5. Constants with Complex Types
  6. Conclusion

Definition

The const keyword declares a variable that is block-scoped and read-only. This means the variable is only available within the block where it is defined and cannot be reassigned a new value after its initial assignment.

Syntax

Syntax

const variableName: type = value;

Example

Here, we declare a constant variable PI using const. The value of PI is set to 3.14 and cannot be changed.

const PI: number = 3.14;
console.log(PI); // Output: 3.14

Block Scope

Variables declared with const are only accessible within the block where they are defined. A block is a section of code enclosed in curly braces {}.

Example

In this example, greeting is declared inside an if block. It is accessible within the block but not outside of it.

if (true) {
  const greeting: string = "Hello, TypeScript!";
  console.log(greeting); // Output: Hello, TypeScript!
}
// console.log(greeting); // Error: Cannot find name 'greeting'.

Reassignment

Variables declared with const cannot be reassigned. Any attempt to reassign a const variable will result in an error.

Example

In this example, trying to reassign the constant MAX_USERS will result in an error.

const MAX_USERS: number = 100;
// MAX_USERS = 150; // Error: Cannot assign to 'MAX_USERS' because it is a constant.
console.log(MAX_USERS); // Output: 100

Constants with Complex Types

While variables declared with const cannot be reassigned, objects and arrays declared with const can still have their contents modified.

Example

In this example, an object user and an array colors are declared with const. While the variable itself cannot be reassigned, the properties of the object and the elements of the array can be changed.

const user = {
  name: "Ramesh",
  age: 25
};

user.age = 26; // Allowed
console.log(user); // Output: { name: 'Ramesh', age: 26 }

const colors = ["red", "green", "blue"];
colors.push("yellow"); // Allowed
console.log(colors); // Output: ['red', 'green', 'blue', 'yellow']

When const is used with objects and arrays, the reference to the object or array cannot be changed, but the properties of the object or the elements of the array can still be modified.

Conclusion

In this chapter, we covered the const keyword in TypeScript, including its definition, syntax, block scope, reassignment rules, and usage with complex types.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare