MENTAL MODEL
Imagine you are a warehouse manager in Patna - 1000 orders have arrived. The same work has to be done on every order - check it, pack it, dispatch it.
There are two options:
Option A - Hire 1000 different people, one for each order.
Option B - One person picks up one order, does the work, then moves to the next one.
Option B = Loop.
A loop exists because:
The same task, different data, repeated again and again - this is the most common pattern in programming.
Without loops, you would write 1000 lines - separate code for every user. The loop said write it once, run it many times.
But here is the twist - a loop is not just “repeat”.
A loop is a controlled iteration machine where you decide:
When to start
When to stop
What to do at each step
If you understand these three things, no form of loop will confuse you.
CORE CONCEPT
for loop - the most fundamental#
for loop - the most fundamental#Three parts:
All other loops are just variations of this:
UNDER THE HOOD
When this loop runs:
The JS engine internally does this:
With var, there is only one i in memory - when setTimeout fires, the loop has already finished, so i = 3.
With let, every iteration creates its own closure - a different i gets captured.
REAL WORLD USAGE
Use Case 1 - GenExpence: Transactions filter + calculate#
Use Case 2 - LMS: Batch certificate generation#
Use Case 3 - CRM: Calculate total for proposal line items#
COMMON MISTAKES
Mistake 2 - Using for...of on an object#
for...of on an object#Mistake 3 - Forgetting await in an async loop#
await in an async loop#Mistake 4 - Modifying an array inside the loop#
EDGE CASES & GOTCHAS
Gotcha 1 - Do not use for...in on arrays#
for...in on arrays#for...in also iterates over the prototype chain.
For arrays, always use for...of.
Gotcha 2 - Infinite loop = production killer#
In production, always keep a safety limit:
Gotcha 3 -var in loop + closure (classic interview trap)#
var in loop + closure (classic interview trap)#Fix 1 - Use let#
let#Fix 2 - IIFE (old approach)#
INTERVIEW SECTION
a) 3 Conceptual Questions#
for...in iterates over an object's enumerable keys, and it may also include properties from the prototype chain. Avoid using it on arrays.
for...of iterates over iterable values (Arrays, Strings, Maps, Sets). It does not work directly on plain objects.
forEach = use it when you need simple iteration, no async/await, and no need for break or continue.
for...of = use it when you need break/continue, or when using async/await inside the loop.
var = function scoped, only one variable exists in memory, and it creates closure issues (classic setTimeout problem).
let = block scoped, every iteration gets its own binding, so closures capture values correctly.
b) 2 Tricky Output Questions#
Q1. What will be the output?#
Answer: 3, 3, 3
Why: var creates only one i, and by the time setTimeout runs, the loop has already finished and i = 3.
Q2. What will be the output?#
Answer: 1, 3
continue skips the current iteration - so 2 is skipped, and the rest are printed.
c) System Design Question#
Your LMS has to send emails to 10,000 students at once after course completion.
How would you design the loop?
Would you directly send all 10k emails in one loop?
Why not?
What approach would you use?
Answer direction:#
Directly doing 10k API calls in one loop = server timeout + rate limit hit
Use batching in chunks of 100
Use a queue system (
Bull/BullMQ) so each job runs separatelyAdd rate limiting - for example, max 10 emails/second
Add retry logic for failed ones