#356 Hoisting

easy
javascript

What will be the output of following code:

const a = 1
console.log(a)

var b
console.log(b)
b = 2

console.log(c)
var c = 3

console.log(d)
let d = 2

Source

1. const a = 1

No output here — just declares and initializes a to 1.

2. console.log(a)

Output: 1
Why: a is a const variable already initialized with the value 1, so logging it returns 1.

3. var b

No output — var is hoisted to the top of the scope and is initialized to undefined by default.

4. console.log(b)

Output: undefined
Why: b exists because of hoisting, but since it hasn’t been assigned 2 yet, it still holds the initial undefined.

5. console.log(c)

Output: undefined
Why: var c is also hoisted and initialized to undefined before code execution begins.
It’s not assigned 3 until the next line, so logging it here prints undefined.

6. console.log(d)

Output: ❌ ReferenceError: Cannot access 'd' before initialization
Why:

  • Variables declared with let (and const) are hoisted but placed in the Temporal Dead Zone (TDZ) until the actual declaration line is executed.
  • Accessing them before that point in the code throws a ReferenceError instead of returning undefined.

Full Output Sequence

When executed, the script prints:

1
undefined
undefined
ReferenceError: Cannot access 'd' before initialization

Key Concepts

  1. var Hoisting

    • Variables are hoisted and initialized to undefined.
    • No error when accessed before declaration, but value will be undefined.
  2. let / const Hoisting + TDZ

    • They are hoisted but stay in the Temporal Dead Zone until the declaration is reached.
    • Accessing during TDZ throws ReferenceError.
  3. const Initialization Requirement

    • Must be initialized at the moment of declaration (unlike let).
  4. Execution Order Matters

    • The position of the declaration and assignment directly impacts output due to hoisting rules.