Variables in JavaScript: var vs let vs const
Variables in JavaScript are declared with var, let or const. var is function-scoped and starts as undefined when hoisted; let and const are block-scoped and throw a ReferenceError if read before their line. const prevents reassignment of the variable, but the object or array it holds can still be changed.
Key Learning Objectives
Declare variables with var, let and const and choose the right one.
Explain function scope vs block scope.
Predict hoisting: undefined for var, ReferenceError for let and const before their line.
Know that const stops reassignment, not mutation of the object it holds.
The Interview Problem
What do the five lines of output say?
1console.log(typeof a, typeof b);2var a = 1;34try {5 console.log(c);6} catch (e) {7 console.log(e.name);8}9let c = 2;1011const obj = { n: 1 };12obj.n = 2;13console.log(obj.n);1415try {16 obj = {};17} catch (e) {18 console.log(e.name);19}2021{22 var fnScoped = 'var';23 let blockScoped = 'let';24}25console.log(fnScoped, typeof blockScoped);
Predict Console Output
Select the option that matches what this code prints to the console:
undefined undefined / ReferenceError / 2 / TypeError / var undefined
1 undefined / ReferenceError / 2 / TypeError / var undefined
undefined undefined / undefined / 2 / TypeError / var undefined
undefined undefined / ReferenceError / 1 / TypeError / var undefined
V8 Engine Execution Trace
Step 1 of 5 (Line 1)var a already exists with the value undefined. b was never declared, and typeof is the one operator that does not throw for it.
Variables in JavaScript Explained
var vs let vs const: Variables in JavaScript
Variables in JavaScript are declared with var, let or const. Use const by default, let when the variable must be reassigned, and avoid var in new code.
- Scope:
varis function-scoped, so it ignores blocks such asif,forand{ }.letandconstare block-scoped. - Hoisting: all three are hoisted, but differently.
varstarts asundefined, so reading it early givesundefined.letandconststay uninitialized until their line runs (the temporal dead zone), and reading them early throws a ReferenceError.typeofan undeclared name returns'undefined'instead of throwing. - const is about the binding:
conststops you from assigning a new value to the variable, but the object or array it holds can still change. UseObject.freezefor a shallow read-only object.varalso allows redeclaring the same name;letandconstdo not.
Common Traps & Mistakes
Thinking const makes an object immutable; only the variable binding is fixed.
Using var inside an if or for block and expecting it to stay inside.
Reading a let or const before its line and expecting undefined.
FAANG Follow-Up Probes
Probe #1
Why does a var loop variable leak into callbacks, and how does let fix it?
Probe #2
What is the temporal dead zone?
Probe #3
How would you make an object and its nested objects read-only?
Variables in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Variables.
What is the difference between var, let and const?
var is function-scoped, can be redeclared and is undefined before its line. let and const are block-scoped, cannot be redeclared in the same scope and throw if used before their line. const also cannot be reassigned.
Can you change a const object?
Yes. const fixes which object the variable points to, not the object's contents, so obj.n = 2 works but obj = {} throws a TypeError. Object.freeze makes the object itself read-only (shallowly).
Are let and const hoisted?
Yes, but they are not initialized. From the start of the block until their line they are in the temporal dead zone, and any access throws a ReferenceError.
Why does typeof an undeclared variable not throw?
typeof was designed to be safe for checking whether a name exists, so typeof notDeclared returns 'undefined'. Reading the name directly throws a ReferenceError, and typeof still throws for let and const in their temporal dead zone.
Which should I use: var, let or const?
Use const by default, let when you need to reassign, and avoid var in new code because its function scope and hoisting cause bugs.
What happens if you assign to a variable that was never declared?
In sloppy mode it silently creates a global variable; in strict mode (and in ES modules and classes) it throws a ReferenceError.
More JavaScript interview questions for freshers
