Category: java
Scope Difference
Published on 18 Feb 2026
Explanation
var has function scope, while let and const
have block scope.
Code:
function test() {
if (true) {
var a = 1;
let b = 2;
const c = 3;
}
console.log(a); // Works
// console.log(b); // Error
// console.log(c); // Error
}
Explanation
var allows redeclaration in the same scope. let
and const do not allow redeclaration.
Code:
var x = 10; var x = 20; // Allowed let y = 10; // let y = 20; // Error const z = 10; // const z = 20; // Error
Explanation
var and let allow reassignment. const does not
allow reassignment after initialization.
Code:
var a = 1; a = 2; // Allowed let b = 1; b = 2; // Allowed const c = 1; // c = 2; // Error
Explanation
var is hoisted and initialized with undefined. let
and const are hoisted but not initialized, leading
to Temporal Dead Zone.
Code:
console.log(a); // undefined var a = 10; // console.log(b); // Error let b = 20;
Explanation
const must be initialized at declaration. var and
let can be declared without initialization.
Code:
var a; let b; // const c; // Error const c = 5;
Explanation
Use const by default. Use let if the
value needs to change. Avoid var in modern
JavaScript.
Code:
const pi = 3.14; let counter = 0; counter++;