HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Let

The let keyword is used to declare variables in modern JavaScript. It is a safer and clearer alternative to var in many situations, especially because let respects block scope and helps reduce common coding mistakes.


What Is let in JavaScript?

let creates a variable whose value can change later. If you need to store a value now and update it later, let is often the right choice.

let score = 10;
score = 15;

console.log(score);

In this example, the variable score is first set to 10 and then updated to 15.


Declaring Variables with let

You can declare a variable with let and assign a value at the same time:

let userName = "Rafi";
let price = 120;
let isLoggedIn = true;

You can also declare the variable first and assign the value later:

let city;
city = "Chattogram";

Variables Declared with let Can Be Reassigned

A variable created with let can receive a new value after declaration. This makes it useful when the value may change while the program runs.

let temperature = 28;
temperature = 30;
Tip: Use let when you know the variable may need a new value later. If the value should never change, const is usually a better choice.

Block Scope of let

One of the biggest advantages of let is block scope. A variable declared with let exists only inside the block where it was created. A block is usually anything inside curly braces { }, such as an if statement or a loop.

if (true) {
    let message = "Hello";
    console.log(message);
}

// console.log(message); // Error

Here, message works only inside the if block. Outside that block, the variable does not exist.

Note: Block scope helps prevent variables from leaking into places where they are not needed. This makes code easier to manage and reduces accidental conflicts.

let Cannot Be Redeclared in the Same Block

You cannot declare the same variable name twice with let inside the same block.

let name = "Mila";
// let name = "Sara"; // Error

This rule helps catch mistakes early. It prevents you from accidentally creating two variables with the same name in one place.


let vs var

Older JavaScript code often uses var, but modern JavaScript usually prefers let. The biggest difference is scope.

Feature let var
Scope Block scope Function scope
Redeclaration in same scope Not allowed Allowed
Modern usage Recommended Mostly older code

A Practical Example

Here is a simple example showing why let is useful when a value changes over time:

let cartTotal = 0;

cartTotal = cartTotal + 200;
cartTotal = cartTotal + 150;

console.log(cartTotal);

The value of cartTotal changes as more items are added, so let fits this situation well.


Key Points to Remember

  • let is used to declare variables in modern JavaScript
  • Variables declared with let can be reassigned
  • let is block-scoped, so it only works inside its own block
  • You cannot redeclare the same let variable in the same block
  • let is usually preferred over var in modern code
  • Use let when the stored value may change later