Lesson 11 of 60 – let and const
18%

let and const in TypeScript

In TypeScript, let and const are used to declare variables. They work similarly to JavaScript, but TypeScript can also use type information to make your code safer.

Note: Use let when the value of a variable needs to change. Use const when the variable should not be reassigned.

1. What is let?

The let keyword is used to declare a variable whose value can be changed later.

let age = 20;

age = 21;

console.log(age);

Here, the value of age changes from 20 to 21.

2. Basic let Syntax

The basic syntax is:

let variableName = value;

Example:

let name = "Rahul";
let age = 22;

3. let with Type Annotation

TypeScript allows you to specify the type of a variable explicitly.

let name: string = "Rahul";

let age: number = 22;

let isStudent: boolean = true;

The type annotation tells TypeScript what type of value the variable should contain.

4. Changing a let Variable

A variable declared with let can be reassigned.

let city: string = "Patna";

city = "Aurangabad";

console.log(city);

The variable still has the type string.

5. let Does Not Allow Different Types

If TypeScript infers that a let variable contains a string, you cannot later assign a number to it.

let name = "Rahul";

name = "Amit";

// This causes a TypeScript error
// name = 100;

The inferred type of name is string.

6. What is const?

The const keyword is used to declare a variable that cannot be reassigned after it has been initialized.

const country = "India";

console.log(country);

7. Basic const Syntax

The basic syntax is:

const variableName = value;

Example:

const pi = 3.14;

const name = "Rahul";

8. const Must Be Initialized

A const variable must receive a value when it is declared.

const age = 20;

This is not valid:

// Invalid
// const age;

You cannot declare a normal const variable without an initial value.

9. const Cannot Be Reassigned

A const variable cannot be assigned another value.

const age = 20;

// Error
// age = 25;

Once initialized, the variable cannot be reassigned.

10. const with Type Annotation

You can also specify the type of a const variable.

const name: string = "Amit";

const age: number = 25;

const isActive: boolean = true;

11. let vs const

Feature let const
Can be reassigned Yes No
Must be initialized No Yes
Block scoped Yes Yes
Common use Changing values Fixed reference

12. let for Changing Values

Use let when a value needs to change.

let score: number = 50;

score = 60;

score = 80;

console.log(score);

The final value is 80.

13. const for Fixed Values

Use const when the variable should not be reassigned.

const collegeName: string = "ABC College";

console.log(collegeName);

The reference stored in collegeName cannot be reassigned.

14. Block Scope

Both let and const are block scoped. A block is commonly represented by curly braces { }.

{
    let message = "Hello";

    console.log(message);
}

The variable exists inside its block.

15. let Inside an if Block

if (true) {

    let message: string = "Welcome";

    console.log(message);

}

// message cannot be accessed here

The let variable belongs to the if block.

16. const Inside an if Block

if (true) {

    const message: string = "Hello";

    console.log(message);

}

// message cannot be accessed here

The same block-scoping rule applies to const.

17. let Inside a Loop

let is commonly used as a loop variable.

for (let i = 1; i <= 5; i++) {

    console.log(i);

}

The variable i belongs to the loop block.

18. const Inside a Loop

A const variable can also be created inside a loop.

const students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

for (const student of students) {

    console.log(student);

}

Each iteration creates a block-scoped student binding.

19. let with Arrays

A let array variable can be reassigned to another array of the same type.

let numbers: number[] = [10, 20, 30];

numbers = [40, 50, 60];

console.log(numbers);

20. const with Arrays

A const array cannot be reassigned to another array. However, its elements can normally be changed.

const numbers: number[] = [10, 20, 30];

numbers[0] = 100;

console.log(numbers);

The array reference remains the same.

21. const with Objects

A const object cannot be reassigned to another object, but its properties can normally be changed.

const student = {
    name: "Rahul",
    age: 20
};

student.age = 21;

console.log(student);

22. let and User Input

In applications, let can be useful when a value changes during program execution.

let userName: string = "Amit";

userName = "Rahul";

console.log(userName);

This pattern is useful when application state or a value changes.

23. const in React Components

In React and React Native code, const is commonly used for values and functions that are not reassigned.

const userName: string = "Rahul";

const message = "Welcome " + userName;

console.log(message);

24. let and React Native Logic

let can be useful for temporary values inside functions or calculations in React Native applications.

function calculateTotal(
    price: number,
    quantity: number
) {

    let total: number;

    total = price * quantity;

    return total;
}

25. Prefer const When Possible

When you do not need to reassign a variable, const is usually a clear choice.

const name: string = "Amit";

console.log(name);

There is no need to use let when the variable will never be reassigned.

26. When Should You Use let?

Use let when you know that the variable needs to be reassigned.

let count: number = 0;

count = count + 1;

count = count + 1;

console.log(count);

Here, count changes during program execution.

27. When Should You Use const?

Use const when the variable will not be reassigned.

const appName: string = "Student App";

const version: number = 1;

console.log(appName);
console.log(version);

28. Complete let and const Example

const studentName: string = "Rahul";

let marks: number = 70;

marks = 85;

const passed: boolean = marks >= 40;

console.log("Name:", studentName);
console.log("Marks:", marks);
console.log("Passed:", passed);

Here, studentName and passed are not reassigned, while marks changes.

29. Common Mistakes with let and const

Mistake 1: Trying to reassign a const variable.

const age = 20;

// Error
// age = 25;

Mistake 2: Declaring const without initialization.

// Error
// const name;

Mistake 3: Assigning an incompatible type.

let age: number = 20;

// Error
// age = "twenty";

30. let and const Summary

Keyword Reassignment Initialization Common Use
let Allowed Can be declared first Changing values
const Not allowed Required at declaration Values that are not reassigned
Remember: Use let for variables that need reassignment and const when the variable should not be reassigned. Both are block-scoped.

📌 Key Points

  • let is used for variables whose values can change.
  • const is used for variables that cannot be reassigned.
  • A const variable must be initialized when declared.
  • Both let and const are block scoped.
  • TypeScript can infer the type of variables declared with let and const.
  • You can explicitly provide type annotations.
  • Use let when reassignment is required.
  • Prefer const when reassignment is not required.
  • Arrays and objects declared with const can still have their contents changed.
  • let and const are widely used in React and React Native TypeScript projects.

🧠 Quick Quiz

Question: Which keyword should you use when a variable needs to be reassigned?