Tutorials Modern JavaScript (ES6+) for Beginners

for...of, Sets & Maps Essentials

Learn for...of, Sets & Maps Essentials in our free Modern JavaScript (ES6+) for Beginners series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
for...of, Sets & Maps Essentials
Lesson 9 of 12 · Part 3 — Data & Collections · Modern JavaScript (ES6+) for Beginners
Course: Modern JavaScript (ES6+) for Beginners · Lesson: 9/12 · Read time: ~15 min · Level: Beginner · ES version: ES2015 (ES6) and above

for...of, Sets & Maps Essentials

ES6 added cleaner loops and two collection types: Set (unique values) and Map (key-value with any key type). Useful for tags, permissions, and caching.

for...of — loop values cleanly

const lessons = ['let/const', 'arrows', 'async'];

for (const lesson of lessons) {
  console.log('Study:', lesson);
}

for (const char of 'ES6') {
  console.log(char);  // E, S, 6
}

Use for...of on arrays and strings. Use for...in only for object keys (less common in modern code).

Set — unique list

const tags = new Set(['javascript', 'es6', 'react', 'es6']);
tags.add('node');
tags.has('react');  // true
tags.size;          // 4 — duplicate 'es6' stored once

const uniqueEmails = [...new Set(emailList)];

Map — dictionary with any keys

const lessonProgress = new Map();
lessonProgress.set('user-42', { completed: 5, total: 12 });
lessonProgress.set('user-99', { completed: 12, total: 12 });

lessonProgress.get('user-42');  // { completed: 5, total: 12 }
🌍 Real-world example — Track unique visitors per day
function countUniqueVisitors(visitLog) {
  const visitors = new Set(visitLog.map(v => v.userId));
  return visitors.size;
}

countUniqueVisitors([
  { userId: 'a1', page: '/tutorials' },
  { userId: 'a2', page: '/ebooks' },
  { userId: 'a1', page: '/blogs' }
]); // 2 unique users
💡 Tip: Convert Set to array: [...mySet] — spread works on iterables.
⚠️ Common Mistake: Using Set or Map without new: Set() throws — always new Set().
👨‍🏫 Teaching note: Compare object {} vs Map when keys are dynamic strings vs numeric IDs — Map wins for frequent add/delete.

Continue learning

Previous: Array Methods — map, filter, find & reduce

Next: Promises — then, catch & finally

Course home: All 12 lessons

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain JavaScript in the context of Modern JavaScript (ES6+) for Beginners.
Short answer: JavaScript runs single-threaded with an event loop. Closures capture lexical scope; promises/async handle I/O without blocking the UI thread. How to structure your answer (60–90 seconds) Define JavaScript i…
Mid Detailed
What are common mistakes teams make with Components when using Modern JavaScript (ES6+) for Beginners?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Components in plain language…
Senior Detailed
How would you debug a production issue related to State in a Modern JavaScript (ES6+) for Beginners application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define State in plain language for…
Mid Detailed
Compare two approaches to API integration—when would you choose each?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define API integration in plain lan…
Junior Detailed
Describe a real-world scenario where Performance mattered in a Modern JavaScript (ES6+) for Beginners project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. How to structure your answer (60–90 seconds) Define Performance in plain languag…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Modern JavaScript (ES6+) for Beginners
Course syllabus
Part 1 — Modern Basics
Part 2 — Write Less Code
Part 3 — Data & Collections
Part 4 — Async & Modules
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details