Iteration and Control Flow
Iteration is the repeated execution of a set of statements. JavaScript offers multiple ways to loop through data, each suited for different data structures and performance requirements.
1. Traditional Loops: for and while
These provide the most granular control over the iteration process.
forloop: Best for iterating a specific number of times.whileloop: Best when the number of iterations is dependent on a dynamic condition.do...while: Ensures the code block runs at least once before checking the condition.
2. Iterating over Objects and Arrays
Modern JavaScript provides specialized loops for different data types:
I. for...of (The Modern Standard)
Used for iterating over Iterables (Arrays, Strings, Sets, Maps). It provides the actual Value of each element.
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
II. for...in (The Object Explorer)
Used for iterating over the Enumerable Properties (Keys) of an object.
- Warning: Do not use this for arrays! It iterates over the indices (as strings) and may include inherited properties from the prototype chain.
3. Functional Iteration: forEach
The forEach method is a built-in array method that executes a provided function once for each array element.
fruits.forEach((fruit, index) => {
console.log(`${index}: ${fruit}`);
});
- Limitation: You cannot use
breakorcontinueinside aforEach. If you need to stop early, use afor...ofloop.
4. Performance and Efficiency
- O(n) Complexity: Most loops are linear in time complexity relative to the size of the dataset.
- Optimization: For massive arrays, a traditional
forloop with a cached length (let i=0, len=arr.length) is historically the fastest, though modern engines (V8) have optimizedfor...ofto be nearly identical in speed.
Interview Pro-Tips: The for...in trap
If an interviewer asks why you shouldn't use for...in for arrays:
- It iterates over the Keys (0, 1, 2 as strings), not the values.
- It is slower because it traverses the entire Prototype Chain.
- The iteration order is not guaranteed to be consistent across different engines.
Always use
for...oforforEachfor arrays.
Technical Summary
Iterable: An object that implements the[Symbol.iterator]method.Control: Usebreakto exit early andcontinueto skip the rest of the current iteration.Paradigm: Functional methods (map,filter,reduce) are often preferred over manual loops for better readability and declustered state.