Variables in JavaScript: var vs let vs const

Easy100% Free~12 mins#variables#var-let-const#block-scope#tdz#const
Interview Answer (30-Second Summary)

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;
3
4try {
5 console.log(c);
6} catch (e) {
7 console.log(e.name);
8}
9let c = 2;
10
11const obj = { n: 1 };
12obj.n = 2;
13console.log(obj.n);
14
15try {
16 obj = {};
17} catch (e) {
18 console.log(e.name);
19}
20
21{
22 var fnScoped = 'var';
23 let blockScoped = 'let';
24}
25console.log(fnScoped, typeof blockScoped);
Predict Console Output
Interactive Challenge

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.

Call Stack (Top = Active)
Global
Lexical Scope / Bindings
a:undefined (var, hoisted)
c:<uninitialized> (TDZ)
obj:<uninitialized> (TDZ)
Console Stream
> undefined undefined

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.

  1. Scope: var is function-scoped, so it ignores blocks such as if, for and { }. let and const are block-scoped.
  2. Hoisting: all three are hoisted, but differently. var starts as undefined, so reading it early gives undefined. let and const stay uninitialized until their line runs (the temporal dead zone), and reading them early throws a ReferenceError. typeof an undeclared name returns 'undefined' instead of throwing.
  3. const is about the binding: const stops you from assigning a new value to the variable, but the object or array it holds can still change. Use Object.freeze for a shallow read-only object. var also allows redeclaring the same name; let and const do 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.

Q1.
What are variables in JavaScript?

Named references to values, declared with var, let or const. A variable can hold any type, and the type can change when it is reassigned.

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.

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).

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.

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.

Use const by default, let when you need to reassign, and avoid var in new code because its function scope and hoisting cause bugs.

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