Block scope in Javascript - Time & Space Complexity
Let's explore how the time it takes to run code changes when we use block scope in JavaScript.
We want to see how the code's speed changes as the input size grows.
Analyze the time complexity of the following code snippet.
function printNumbers(n) {
for (let i = 0; i < n; i++) {
{
let temp = i * 2;
console.log(temp);
}
}
}
This code prints double the value of numbers from 0 up to n-1 using block scope inside the loop.
- Primary operation: The for-loop runs and prints a value each time.
- How many times: It runs exactly n times, once for each number from 0 to n-1.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times |
| 100 | 100 times |
| 1000 | 1000 times |
Pattern observation: The number of operations grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the input size.
[X] Wrong: "Using block scope inside the loop makes the code slower because it creates variables every time."
[OK] Correct: Creating variables inside a block is very fast and does not add extra loops or repeated heavy work. The main time is spent in the loop itself.
Understanding how loops and block scope affect time helps you explain code efficiency clearly and confidently.
"What if we added a nested loop inside the block scope? How would the time complexity change?"
Practice
{ } in JavaScript?Solution
Step 1: Understand block scope
Block scope means variables exist only inside curly braces{ }.Step 2: Identify keywords that create block scope
letandconstcreate block-scoped variables, whilevardoes not.Final Answer:
let-> Option CQuick Check:
Block scope =let[OK]
let and const are block scoped [OK]- Thinking
varis block scoped - Confusing
functionwith variable declaration - Assuming global is a keyword
score with value 10?Solution
Step 1: Identify constant declaration syntax
To declare a constant, useconstfollowed by the variable name and value.Step 2: Check block scope and immutability
constcreates a block-scoped variable that cannot be reassigned.Final Answer:
const score = 10;-> Option DQuick Check:
Constant declaration =const[OK]
const for constants, let for variables [OK]- Using
varwhich is not block scoped - Omitting
letorconst - Trying to reassign a
constvariable
if (true) {
let x = 5;
}
console.log(typeof x);Solution
Step 1: Understand variable scope of
The variableletxis declared withletinside theifblock, so it exists only inside that block.Step 2: Check access outside the block
typeofon an undeclared variable returns"undefined". Direct access likeconsole.log(x)would throwReferenceError.Final Answer:
"undefined" -> Option AQuick Check:
typeofundeclared ="undefined"[OK]
typeof returns "undefined" for undeclared vars, no error [OK]- Thinking it throws ReferenceError (confuses with direct access)
- Confusing
varandletscope - Expecting "number"
if (true) {
var y = 10;
}
console.log(y);Solution
Step 1: Understand
varscopevaris function-scoped or global-scoped, not block-scoped, soyis accessible outside theifblock.Step 2: Check console output
Sinceyis defined,console.log(y)outputs 10 without error.Final Answer:
No error, outputs 10 -> Option BQuick Check:
varignores block scope [OK]
var ignores block scope, accessible outside blocks [OK]- Expecting ReferenceError for
var - Confusing block scope with function scope
- Thinking semicolon missing causes error
function test() {
if (true) {
let a = 1;
var b = 2;
}
console.log(b);
console.log(a);
}
test();What will happen when this code runs?
Solution
Step 1: Analyze
let ascopeais declared withletinside theifblock, so it is not accessible outside that block.Step 2: Analyze
var bscopebis declared withvar, which is function-scoped, so it is accessible anywhere insidetest().Step 3: Predict console output
Loggingboutputs 2 because it is function-scoped. Loggingacauses aReferenceErrorbecause it is out of scope.Final Answer:
Logs 2, then ReferenceError fora-> Option AQuick Check:
letblock scope hidesa,varfunction scope showsb[OK]
let is block scoped; var is function scoped [OK]- Expecting both variables accessible
- Confusing
letandvarscopes - Thinking mixed declarations cause syntax error
Ready to prove it?
Pass the test and this pattern moves unknown
