CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ” Understanding `of` in JavaScript – `for...of` Loop Deep Dive

The for...of loop iterates over values in iterable objects like arrays and strings. Modern alternative to traditional for loops with cleaner syntax.

In JavaScript, the keyword is used in the loopβ€” not an operator , but a language construct that allows iteration over iterable objects. -- πŸ“Œ Syntax : variable that holds the current value : object with an internal iterator (e.g. , , , etc.) -- βœ… Supported Iterables Works with βœ… βœ… βœ… (browser) -- πŸ” Example: Array -- πŸ” Example: String -- πŸ” Example: Map -- 🚫 vs Values ❌ (unless iterable) βœ… Recommended Preserved (iterators) -- πŸ§ͺ Example Difference: -- πŸ” Under the Hood uses the iterable protocol. When you do: JS internally does: -- πŸ›  When to Use You want values, not keys. You’re dealing with iterable data (arrays, strings, sets, maps). You want cleaner syntax than or manual loops. You need , , or β€”which doesn't support. -- ⚠️ Caveats Doesn't work on plain objects unless you make them iterable. Can’t use async insideβ€”use for that. -- βœ… Conclusion The construct is the canonical way to iterate over iterable values in modern JavaScript. It's readable, concise, and robustβ€” avoid for arrays or iterable data structures . -- <!-quiz-start --Q1: What does iterate over? [ ] Object keys [ ] Array indexes [x] Iterable values [ ] Object properties including inherited ones Q2: Which of the following can you iterate with ? [ ] Plain objects [x] Arrays, Strings, Maps, and Sets [ ] Only arrays [ ] Only strings Q3: What is the key difference between and ? [ ] is faster than [ ] only works on arrays [x] iterates over values, while iterates over keys/property names [ ] There is no difference, they are interchangeable <!-quiz-end --
JavaScriptCore Concepts
πŸ›‘ AbortController: Canceling Async Operations in JavaScript
medium
πŸ”’ Closures in JavaScript β€” The Complete Guide
hard
πŸ“¦ Understanding ES6 Modules in JavaScript
medium
⚑ JavaScript Event Loop: Complete Guide to Asynchronous Execution
hard
🧭 Arrow Functions vs Function Declarations in JavaScript
easy
πŸ—‘οΈ Garbage Collection in JavaScript β€” Memory Management & Leak Prevention
hard
πŸ—οΈ Constructor Functions in JavaScript
medium
πŸ‘οΈ MutationObserver: Watching DOM Changes in JavaScript
medium
πŸ” Understanding `of` in JavaScript – `for...of` Loop Deep Dive
hard
πŸ”— Prototype and Prototype Inheritance in JavaScript
medium
πŸ•΅οΈ What Are Proxies in JavaScript? (With Practical Use Cases)
medium
🎯 Scope in JavaScript β€” The Complete Guide
hard
πŸ”„ Script Loading: async vs defer vs Both
hard
πŸ“€ JavaScript Spread Operator (...) Explained
easy
🎯 The JavaScript `this` Keyword: Complete Guide to Context Binding
medium
9 of 15
LibraryJavaScriptCore Concepts9 of 61

πŸ” Understanding `of` in JavaScript – `for...of` Loop Deep Dive

jsgeneral-conceptshard

In JavaScript, the keyword of is used in the for...of loopβ€” not an operator , but a language construct that allows iteration over iterable objects.


πŸ“Œ Syntax

for (const element of iterable) {
  // code block
}
  • element: variable that holds the current value
  • iterable: object with an internal iterator (e.g. Array, String, Map, etc.)

βœ… Supported Iterables

TypeWorks with for...of
Arrayβœ…
Stringβœ…
Map,Setβœ…
argumentsβœ… (in ES6+)
NodeListβœ… (browser)
Object❌ (not iterable)

πŸ” Example: Array

const numbers = [10, 20, 30];

for (const num of numbers) {
  console.log(num); // 10, then 20, then 30
}

πŸ” Example: String

for (const char of 'Hi') {
  console.log(char); // 'H', 'i'
}

πŸ” Example: Map

const map = new Map([['a', 1], ['b', 2]]);

for (const [key, value] of map) {
  console.log(key, value); // 'a' 1, then 'b' 2
}

🚫 for...of vs for...in

Featurefor...offor...in
Iterates overValuesKeys (property names)
Works on objects❌ (unless iterable)βœ…
Use for arraysβœ… Recommended❌ Avoid β€” includes inherited keys
OrderPreserved (iterators)Not guaranteed

πŸ§ͺ Example Difference:

const arr = ['a', 'b'];

for (const i in arr) {
  console.log(i); // 0, 1 (indexes as strings)
}

for (const val of arr) {
  console.log(val); // 'a', 'b' (actual values)
}

πŸ” Under the Hood

for...of uses the iterable protocol. When you do:

for (const x of iterable) {}

JS internally does:

const iterator = iterable[Symbol.iterator]();
let result;
while (!(result = iterator.next()).done) {
  const x = result.value;
  // loop body
}

πŸ›  When to Use for...of

  • You want values, not keys.
  • You’re dealing with iterable data (arrays, strings, sets, maps).
  • You want cleaner syntax than .forEach() or manual for loops.
  • You need break, continue, or returnβ€”which forEach doesn't support.

⚠️ Caveats

  • Doesn't work on plain objects unless you make them iterable.
  • Can’t use async await insideβ€”use for await...of for that.

βœ… Conclusion

The for...of construct is the canonical way to iterate over iterable values in modern JavaScript. It's readable, concise, and robustβ€” avoid for...in for arrays or iterable data structures .


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `for...of` iterate over?
Q2Which of the following can you iterate with `for...of`?
Q3What is the key difference between `for...of` and `for...in`?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna