Category: React • Beginner
Published on 18 Feb 2026
Explanation
#white-var has function scope, while let and const #white-have block scope.
Code Example
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
#white-var allows redeclaration in the same scope. let #white-and const do not allow redeclaration.
Code Example
var x = 10; var x = 20; // Allowed let y = 10; // let y = 20; // Error const z = 10; // const z = 20; // Error
Explanation
#white-var and let allow reassignment. const does not #white-allow reassignment after initialization.
Code Example
var a = 1; a = 2; // Allowed let b = 1; b = 2; // Allowed const c = 1; // c = 2; // Error
Explanation
#white-var is hoisted and initialized with undefined. let #white-and const are hoisted but not initialized, leading #white-to Temporal Dead Zone.
Code Example
console.log(a); // undefined var a = 10; // console.log(b); // Error let b = 20;
Explanation
#white-const must be initialized at declaration. var and #white-let can be declared without initialization.
Code Example
var a; let b; // const c; // Error const c = 5;
Explanation
#white-Use const by default. Use let if the #white-value needs to change. Avoid var in modern #white-JavaScript.
Code Example
const pi = 3.14; let counter = 0; counter++;