JavaScript Academy · Lesson

Iteration Patterns

Iterate Map and Set with for...of; use entries/keys/values; convert to arrays; build tiny counters and unique lists.

Lesson 3 of 38 steps

Iteration Patterns is a free JavaScript Academy lesson on CoddyKit. This is lesson 3 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.

Iteration overview

Goal: Learn simple, clear iteration.

  • Map: entries, keys, values
  • Set: values
  • Convert to arrays
  • Frequency counter & unique lists
Iteration Patterns — illustration 1

Map: entries/keys/values

Use for...of on a Map to get [key, value]; keys() and values() are also available.

// Map iterates as [key, value] pairs
const prices = new Map();
prices.set("apple", 3);
prices.set("banana", 2);

for (const [item, cost] of prices) {
  console.log(item, cost);
}

// You can also use keys() or values()
for (const k of prices.keys()) {
  console.log("key:", k);
}
for (const v of prices.values()) {
  console.log("value:", v);
}
Iteration Patterns — illustration 2

Set: simple loop

Set yields unique values in insertion order; duplicates are ignored.

// Set iterates values in insertion order
const colors = new Set(["red", "green", "red", "blue"]);

for (const c of colors) {
  console.log("color:", c);
}

console.log("size:", colors.size);
Iteration Patterns — illustration 3

Conversions with Array.from

Use Array.from to turn Maps/Sets into arrays (and pairs into a Map).

// Convert Map to array of pairs, and back
const map1 = new Map([["a", 1], ["b", 2]]);
const pairs = Array.from(map1); // [["a",1],["b",2]]
console.log("pairs:", pairs);

const map2 = new Map(pairs);
console.log("map2 has a?", map2.has("a"));

// Set to array of values
const set1 = new Set([1, 1, 2, 3]);
const arr = Array.from(set1); // [1,2,3]
console.log("arr:", arr);
Iteration Patterns — illustration 4

Frequency counter

A Map counter is a clear way to tally occurrences in a list.

// Count items in an array using Map
function countItems(list) {
  const counts = new Map();
  for (const x of list) {
    const n = counts.get(x) || 0;
    counts.set(x, n + 1);
  }
  return counts;
}

const fruits = ["apple", "banana", "apple", "apple", "banana"];
const counts = countItems(fruits);

// Print counts as "item: n"
for (const [name, n] of counts) {
  console.log(name + ":", n);
}
Iteration Patterns — illustration 5

Unique + membership

Use Set to build a unique list and for fast membership checks with has.

// Unique values with Set, quick membership test
const numbers = [2, 3, 2, 4, 3, 5];
const unique = Array.from(new Set(numbers));
console.log("unique:", unique);

// Quick membership with Set.has
const allowed = new Set([2, 5]);
console.log("allowed has 2?", allowed.has(2));
console.log("allowed has 4?", allowed.has(4));
Iteration Patterns — illustration 6

Map iteration quiz

Quick check: Best Map loop for key + value.

Iteration Patterns — illustration 7

Recap

Recap: Iterate Map with for...of to get [key, value], loop Set values directly, and convert with Array.from. Use a Map for counters and a Set for unique lists.

Iteration Patterns — illustration 8
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Iteration Patterns” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Iteration Patterns”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.

What will I learn in “Iteration Patterns”?

Iterate Map and Set with for...of; use entries/keys/values; convert to arrays; build tiny counters and unique lists. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Iteration Patterns” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Map & Set essentials
  2. WeakMap & WeakSet — simple privacy and caches
  3. Iteration Patterns
← Back to JavaScript Academy