๐Ÿงฎ JavaScript Variables & Data Types
Estimated reading: 4 minutes 43 views

๐Ÿง  JavaScript let Keyword โ€“ Block Scope & TDZ Explained

๐Ÿ’ก Ever encountered weird bugs when using var in a loop or block?
Thatโ€™s where let comes to the rescue.

The let keyword in JavaScript allows you to declare block-scoped variablesโ€”ensuring safer, more predictable behavior in modern JS code. With the introduction of ES6, let quickly became the preferred way to declare variables that change over time.


๐Ÿ“˜ What Youโ€™ll Learn

By the end of this guide, youโ€™ll understand:

  • โœ… How let works in JavaScript with real-world examples
  • โœ… The key differences between let and var
  • โœ… The concept of the Temporal Dead Zone (TDZ)
  • โœ… Best practices to avoid common bugs

โœ… let Syntax

let variableName = value;

๐Ÿ” Line-by-Line Explanation:

  • let: The keyword used to declare the variable
  • variableName: The name of your variable
  • = value: Assigns a value to the variable (optional at declaration)

๐Ÿ“Œ Example:

let age = 25;
console.log(age); // Output: 25
  • let age = 25; โ€” Declares a block-scoped variable age and assigns it 25.
  • console.log(age); โ€” Outputs the value 25 to the console.

๐Ÿงฉ Features of let

๐Ÿ”น 1. Block Scope

Variables declared with let are only accessible within the block ({}) theyโ€™re declared in.

{
let x = 10;
console.log(x); // 10 โœ… accessible here
}
console.log(x); // โŒ ReferenceError: x is not defined

๐Ÿ’ก Block scope prevents variables from leaking into other scopesโ€”reducing side effects.


๐Ÿ”น 2. No Redeclaration in Same Scope

You cannot declare the same variable twice in the same block with let.

let name = "Alice";
// let name = "Bob"; โŒ SyntaxError: Identifier 'name' has already been declared

๐Ÿ›‘ This avoids accidental redeclarationsโ€”a common source of bugs with var.


๐Ÿ”น 3. Reassignable

Unlike const, you can reassign values to a let variable.

let score = 90;
score = 95; // โœ… Allowed
console.log(score); // 95

๐Ÿ“Œ let is ideal when the variable value will change later in the program.


๐Ÿ”น 4. Temporal Dead Zone (TDZ)

Variables declared with let exist in a โ€œtemporal dead zoneโ€ from the start of the block until the declaration is processed.

console.log(a); // โŒ ReferenceError
let a = 5;

๐Ÿ’ก Even though a is hoisted, it is not initializedโ€”accessing it before declaration triggers an error.


๐Ÿ”„ let vs var โ€“ Comparison Table

Featureletvar
ScopeBlock-scopedFunction-scoped
RedeclarationโŒ Not allowedโœ… Allowed
HoistingHoisted (TDZ applies)Hoisted (initialized undefined)
Default ValueNot initializedundefined
Use in LoopsSafe (per-iteration scope)Risky (shared scope)

โœ… Use let for safer scoping and cleaner code.


๐Ÿงช Practical Example โ€“ Loop Closure with let vs var

โœ… Using let (Correct output):

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

Explanation:

  • Each iteration creates a new i bound to that specific loop.
  • setTimeout correctly captures the value.

โŒ Using var (Incorrect output):

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

Why this happens:

  • var is function-scoped, so all timeouts share the same i.
  • After the loop ends, i becomes 3, and all functions reference that.

โœ… Best Practices with let

โœ”๏ธ Use let when:

  • The variable needs to be reassigned (e.g., loop counters, flags)
  • You want to limit scope to a block or {} to avoid leakage
  • You want to prevent redeclarations within the same block

โŒ Avoid let when:

  • You need a constant value โ†’ use const
  • You are unaware of scope rules โ†’ it might behave unexpectedly

โ“ Frequently Asked Questions (FAQs)

โ“ Can I use let without assigning a value?

โœ… Yes. You can declare and initialize later:

let user;
user = "John";
console.log(user); // John

โ“ What happens if I access a let variable before it’s declared?

โ›” Youโ€™ll get a ReferenceError due to TDZ:

console.log(a); // โŒ ReferenceError
let a = 10;

โ“ Is let hoisted?

Yes, but itโ€™s not initialized until the line is reached. The time between block start and declaration is the Temporal Dead Zone.


โ“ Can let be used globally?

Yes, but it’s scoped to the script/module, not added to the global window object like var.

let x = 100;
console.log(window.x); // undefined

๐Ÿ“Œ Summary

  • let provides block-level scoping, protecting variables from unintentional usage across blocks.
  • Unlike var, it prevents redeclaration and avoids closure bugs in loops.
  • It introduces the concept of Temporal Dead Zone (TDZ) for safer coding practices.

๐Ÿ‘‰ Mastering let is essential for writing modern, bug-free JavaScript.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript Let

Or Copy Link

CONTENTS
Scroll to Top