Category: javascript
let key word in JS
Published on 14 Sep 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);