Modern JavaScript (ES6+) for Beginners
Lesson 9 of 12 75% of course

for...of, Sets & Maps Essentials

1 · 5 min · 6/7/2026

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.

Sign in to track progress and bookmarks.

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

Test your knowledge

Quizzes linked to this course—pass to earn certificates.

Browse all quizzes
Modern JavaScript (ES6+) for Beginners

On this page

for...of — loop values cleanly Set — unique list Map — dictionary with any keys Continue learning
Part 1 — Modern Basics
Introduction — Why ES6+ Before Node.js & React let, const & Block Scope Arrow Functions Explained
Part 2 — Write Less Code
Template Literals & Modern Strings Destructuring — Arrays & Objects Spread & Rest Operators (...)
Part 3 — Data & Collections
Default Parameters & Object Shorthand Array Methods — map, filter, find & reduce for...of, Sets & Maps Essentials
Part 4 — Async & Modules
Promises — then, catch & finally async/await & Fetch API Modules — import/export & What to Learn Next
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