A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-let/ below:

JavaScript Let - GeeksforGeeks

JavaScript Let

Last Updated : 11 Jul, 2025

The let keyword is a modern way to declare variables in JavaScript and was introduced in ECMAScript 6 (ES6). Unlike var, let provides block-level scoping. This behaviour helps developers avoid unintended issues caused by variable hoisting and scope leakage that are common with var.

Syntax

let variable = value;

The let keyword allows you to declare a variable, assign a value to it, and later modify its value within the defined scope.

Key Features of let 1. Block Scope

Variables declared with let are block-scoped, meaning they are only accessible within the block, statement, or expression where they are defined. This is a significant improvement over var, which has function scope and can lead to unexpected behaviour.

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

Output

Block Scope

Here, x is accessible only within the if block, leading to a ReferenceError when accessed outside of it.

2. No Hoisting Issues

While variables declared with let are hoisted, they are not initialized. This creates a temporal dead zone (TDZ) where accessing the variable before its declaration results in a ReferenceError, helping prevent unintended access.

JavaScript
console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 20;

Output

No Hoisting Issues 3. No Redeclaration

A major benefit of let is that it does not allow redeclaration of the same variable in the same scope. This prevents accidental overwrites of variables, reducing bugs and improving code readability.

JavaScript
let z = 30;
let z = 40; // SyntaxError: Identifier 'z' has already been declared

Output

No Redeclaration

Attempting to redeclare z results in a SyntaxError, which helps catch potential issues early in development.

4. Suitable for Loops

Using let in loops is particularly beneficial because the variable declared with let is scoped to the loop block, and each iteration gets a new instance of the variable.

JavaScript
for (let i = 0; i < 3; i++) {
    setTimeout(() => console.log(i), 1000);
}

Output

Suitable for Loops

Here, let ensures each loop iteration has its own i, so the setTimeout callback correctly logs 0, 1, 2, instead of just 3, which would happen with var.

5. Cleaner Syntax

Because let has block-level scope, it prevents variable declarations from "leaking" outside of their intended scope. This makes code cleaner and easier to debug.

JavaScript
{ 
    let a = 5; 
}
console.log(a); // ReferenceError: a is not defined

Output

Cleaner Syntax

In this example, a is accessible only inside the block, and trying to access it outside results in a ReferenceError.

6. Temporal Dead Zone (TDZ)

When using let, accessing a variable before its declaration results in a Temporal Dead Zone (TDZ), which prevents accidental access to uninitialized variables.

JavaScript
console.log(b); // ReferenceError
let b = 15;

Output

Temporal Dead Zone (TDZ)

This TDZ behavior ensures that variables are not accessed before they are properly initialized, promoting safer coding practices.

7. Safer Closure Behavior

With let, closures work as expected, providing a separate instance of the variable for each iteration in a loop. This avoids the common issue where the last value of a loop variable is captured by closures when using var.

JavaScript
const funcs = [];
for (let i = 0; i < 3; i++) {
    funcs.push(() => console.log(i));
}
funcs[0]();
funcs[1]();
funcs[2]();

Each function in the funcs array captures its own i due to let's block scoping, providing the expected behavior in closures.

8. Integration with Modern JavaScript

The use of let aligns with modern JavaScript best practices, supporting compatibility with modules, destructuring, and ES6+ features.

JavaScript
let { name, age } = { name: "Pranjal", age: 20 };
console.log(name, age);
Interesting facts about let Drawbacks of using let Conclusion

The let keyword is a modern and safer way to declare variables in JavaScript. It eliminates many pitfalls associated with var and provides developers with a clearer and more robust way to manage scope. By understanding and leveraging the features of let, developers can write more maintainable and error-free code.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4