wasAsked.

What is the difference between var, let and const in JavaScript?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · JavaScript

Community answers

Posted by wasAsked editorial collection

var, let and const all declare variables in JavaScript, but they differ in scope, hoisting behaviour, redeclaration and reassignment. var - Function-scoped (or global if declared outside a function); ignores block scope. if (true) { var x = 10; } console.log(x); // 10 — visible outside the block - Hoisted and initialised with undefined, so it can be used before its declaration without an error: console.log(a); // undefined var a = 5; - Can be redeclared in the same scope without an error (var a = 1; var a = 2;). - At the top level of a script, var creates a property on the global object (window.a in browsers). - Legacy; avoid in modern code. let - Block-scoped: visible only inside the nearest { } block (if, for, while or a plain block). if (true) { let y = 10; } console.log(y); // ReferenceError - Hoisted but not initialised: accessing it before the declaration throws a ReferenceError. The period between the start of the scope and the declaration is the Temporal Dead Zone (TDZ). - Cannot be redeclared in the same scope. - Can be reassigned. let count = 1; count = 2; // OK const - Block-scoped and has a TDZ, like let. - Must be initialised at declaration and cannot be reassigned. const PI = 3.14159; PI = 3; // TypeError: Assignment to constant variable - const does not make values immutable: object properties and array contents can still change. const user = { name: 'Asha' }; user.name = 'Ravi'; // allowed user = {}; // TypeError Use Object.freeze (shallow) or immutable patterns if you need immutable objects. Comparison - Scope: var function; let and const block. - Hoisting: var hoisted with undefined; let and const hoisted but uninitialised (TDZ). - Redeclaration in the same scope: var allowed; let and const not allowed. - Reassignment: var and let allowed; const not allowed. - Global object property: var yes; let and const no. Classic loop example for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 3, 3, 3 for (let j = 0; j < 3; j++) setTimeout(() => console.log(j)); // 0, 1, 2 let creates a new binding for each iteration, so each callback captures its own value. Best practice - Use const by default for values that should not be reassigned (most variables, functions, imported values, objects you will mutate but not replace). - Use let only when you need to reassign (counters, accumulators). - Avoid var in modern JavaScript and TypeScript; linters (ESLint no-var, prefer-const) enforce this. Interview summary: "var is function-scoped, hoisted as undefined and can be redeclared. let and const are block-scoped with a temporal dead zone and cannot be redeclared; let can be reassigned, const cannot — though const objects can still be mutated. Prefer const, then let, and avoid var."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics