Select Page

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

August 05, 2025

let - how to use the let keyword in JavaScript


  • 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
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...


let is a modern way to declare variables in JavaScript. It was introduced in ES6 (2015) and is now preferred over var in most situations. For example,

Key Features of let

  • Block-Scoped

A let variable only exists inside the block {} where it’s declared. For example,

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)

let variable can be reassigned. For example,

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

If you try to declare the same variable twice with let in the same block, it will throw an error.

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

Like const, let is hoisted to the top of its block, but not initialized. It stays in a “Temporal Dead Zone (TDZ)” until the declaration line is reached. For example,

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
var – How to use the var keyword in JavaScript

Still using [crayon-68958bcb44e72193435750-i/] in JavaScript? You might be setting silent traps in...



0 Comments

Submit a Comment

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