Hoisting with let and const in Javascript - Time & Space Complexity
Let's explore how JavaScript handles variable declarations with let and const behind the scenes.
We want to understand how the program's steps grow when it runs with these variables.
Analyze the time complexity of the following code snippet.
function example() {
console.log(a); // ReferenceError
let a = 5;
const b = 10;
console.log(b);
}
example();
This code shows how let and const variables behave when accessed before declaration.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated steps here.
- How many times: The code runs a fixed number of steps once.
Since there are no loops or repeated actions, the number of steps stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 steps |
| 100 | 5 steps |
| 1000 | 5 steps |
Pattern observation: The steps do not increase with input size; they stay constant.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is.
[X] Wrong: "Accessing let or const variables before declaration works like var."
[OK] Correct: Unlike var, let and const are in a "temporal dead zone" before their declaration, causing errors if accessed early.
Understanding how JavaScript handles variable declarations helps you write clearer code and avoid tricky bugs, a skill valuable in many coding situations.
"What if we replaced let and const with var? How would the time complexity and behavior change?"
Practice
let variable before its declaration in JavaScript?Solution
Step 1: Understand hoisting behavior of
Variables declared withletletare hoisted but not initialized immediately.Step 2: Access before declaration causes error
Accessing such variables before declaration triggers a ReferenceError due to the temporal dead zone.Final Answer:
It throws a ReferenceError due to the temporal dead zone. -> Option BQuick Check:
Temporal Dead Zone = ReferenceError [OK]
- Thinking it returns undefined like var
- Assuming it returns null
- Believing it has a default value
Solution
Step 1: Recall correct const declaration syntax
The correct way to declare a constant is usingconstfollowed by the variable name and assignment.Step 2: Identify invalid syntax options
Options B, C, and D use invalid keywords or combinations not supported in JavaScript.Final Answer:
const PI = 3.14; -> Option AQuick Check:
const declaration syntax = const name = value [OK]
- Using 'constant' keyword instead of 'const'
- Combining let or var with const
- Missing assignment during declaration
console.log(x); let x = 10;
Solution
Step 1: Analyze variable declaration and usage
The variablexis declared withletafter theconsole.logstatement.Step 2: Understand temporal dead zone effect
Accessingxbefore its declaration causes a ReferenceError due to the temporal dead zone.Final Answer:
ReferenceError -> Option CQuick Check:
let before declaration = ReferenceError [OK]
- Assuming it prints undefined like var
- Expecting the value 10 immediately
- Thinking it prints null
function test() {
console.log(a);
const a = 5;
}
test();Solution
Step 1: Identify the cause of error
Accessingabefore itsconstdeclaration causes a ReferenceError due to temporal dead zone.Step 2: Fix by reordering declaration
Moving theconst a = 5;line beforeconsole.log(a);fixes the error.Final Answer:
ReferenceError, fix by moving const declaration before console.log -> Option DQuick Check:
Declare const before use to avoid ReferenceError [OK]
- Changing const to var without fixing order
- Ignoring temporal dead zone
- Assuming no error occurs
const a = 1;
{
console.log(a);
let a = 2;
}Solution
Step 1: Understand block scope and hoisting
The block declares a newlet awhich is hoisted but uninitialized inside the block.Step 2: Accessing
Inside the block,abeforelet adeclarationconsole.log(a)tries to access the block-scopedabefore its declaration, causing a ReferenceError due to temporal dead zone.Final Answer:
ReferenceError -> Option AQuick Check:
Block-scoped let shadows outer const causing ReferenceError [OK]
- Assuming it prints outer const value 1
- Expecting it prints 2 before declaration
- Thinking it prints undefined
Ready to prove it?
Pass the test and this pattern moves unknown
