Hackforge Academy

Category: javascript

let key word in JS

Published on 18 Feb 2026

Explanation


let is a keyword introduced in ES6 used
to declare variables with block scope.

Code:

let name = "Praveen";
console.log(name);

Explanation


Variables declared with let are limited to the
block in which they are defined (inside {}
braces).

Code:

{
  let x = 10;
  console.log(x);
}
// console.log(x); // Error: x is not defined

Explanation


Variables declared with let can be reassigned with
a new value.

Code:

let count = 5;
count = 10;
console.log(count);

Explanation


You cannot redeclare a variable with let in
the same scope.

Code:

let age = 25;
// let age = 30; // Error: Identifier 'age' has already been declared

Explanation


var has function scope, while let has block
scope. let is safer and avoids scope-related issues.

Code:

if (true) {
  var a = 1;
  let b = 2;
}

console.log(a); // Works
// console.log(b); // Error

Explanation


Variables declared with let cannot be accessed before
their declaration due to the Temporal Dead Zone.

Code:

// console.log(x); // Error
let x = 10;
console.log(x);

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community