let – How to Use the ‘let’ Keyword in JavaScript
Still using var in 2025? It’s time to level up. Discover why let is the smarter, safer way to declare variables in modern JavaScript—and how it can save your code from hidden bugs.
nanadwumor

-
let is block-scoped, meaning it only exists inside {} where it’s declared.
-
It can be reassigned but cannot be redeclared in the same scope.
-
let is hoisted, but stays in a Temporal Dead Zone (TDZ) until initialized.
-
Safer than var and preferred in modern JavaScript since ES6 (2015).
RECOMMENDED ARTICLES
var – How to use the var keyword in JavaScript
Still using [crayon-68958bcb44e72193435750-i/] in JavaScript? You might be setting silent traps in your code. Discover why modern developers are ditching [crayon-68958bcb44e85089610352-i/] for safer, smarter alternatives like [crayon-68958bcb44e89100561601-i/] and...
What is the difference between strict and non-strict modes in Javascript?
If you ask your code to be executed under strict mode, JavaScript will force you to declare your variables else it will throw an exception or call foul that your code has error. Javascript can be executed in a strict mode just as a language like...
1 |
let name = "Alice"; |
Key Features of let
-
Block-Scoped
1 2 3 4 5 |
{ let age = 30; console.log(age); // 30 } console.log(age); // ❌ ReferenceError |
Output
30
ReferenceError: age is not defined
Explanation
-
The variable age is declared with let inside a block {}, so it’s block-scoped.
-
Inside the block, age exists and prints 30.
-
Outside the block, age doesn’t exist, so trying to access it throws a ReferenceError.
🔒
Memory Aid: Think of
let as a net. Once a variable is caught inside a block, it’s not let out.
So
let is block-scoped and vanishes outside its curly braces.
-
Can Be Updated (Reassigned)
1 2 |
let count = 1; count = 2; // works |
Output
2
Explanation
-
let allows reassignment of values after declaration.
-
The variable count starts with 1, then gets updated to 2.
-
Cannot Be Redeclared in Same Scope
1 2 |
let x = 5; let x = 10; // ❌ SyntaxError |
Output
SyntaxError: Identifier ‘x’ has already been declared
Explanation
-
let does not allow redeclaration of the same variable in the same scope.
-
Once you’ve declared x with let, you can reassign it (e.g., x = 10) but not redeclare it.
-
Hoisted but in Temporal Dead Zone
1 2 |
console.log(score); // ❌ ReferenceError let score = 100; |
Output
ReferenceError: Cannot access ‘score’ before initialization
Explanation
-
let is hoisted to the top of its block but not initialized.
-
It remains in a special state called the Temporal Dead Zone (TDZ) until the line where it’s declared.
-
Accessing it before declaration throws a ReferenceError, not undefined like with var.
You May Also Like…
var – How to use the var keyword in JavaScript
Still using [crayon-68958bcb44e72193435750-i/] in JavaScript? You might be setting silent traps in...
What is the difference between strict and non-strict modes in Javascript?
If you ask your code to be executed under strict mode, JavaScript will force you to declare your...
0 Comments