Generator functions, yield, and small pipelines
Write generator functions with function*, use yield to produce values lazily, drive them with for...of or next(), and compose tiny pipelines.
Generator functions, yield, and small pipelines is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why generators?
Goal: Produce values lazily with generators.
- function* defines a generator
- yield produces values step by step
- Use for...of or next() to consume
- Compose tiny pipelines (map-like)

function* and yield
Generators use function* and yield. Iteration resumes from the last yield.
// A basic generator that yields 1..n lazily
function* countTo(n) {
for (let i = 1; i <= n; i = i + 1) {
yield i; // pause here and give i to the caller
}
}
for (const x of countTo(3)) {
console.log("value:", x);
}

Manual next()
next() steps through yields and finally returns done: true when finished.
// You can also drive a generator manually with next()
const it = countTo(2);
console.log(it.next()); // { value: 1, done: false }
console.log(it.next()); // { value: 2, done: false }
console.log(it.next()); // { value: undefined, done: true }

Even numbers lazily
Generators are great for simple streams like even numbers without building big arrays first.
// Yield even numbers up to n (inclusive)
function* evensUpTo(n) {
for (let i = 0; i <= n; i = i + 1) {
if (i % 2 === 0) {
yield i;
}
}
}
console.log("evens 0..6:", [...evensUpTo(6)]);

Lazy map pipeline
Compose small steps: feed one generator into another to build lazy pipelines.
// map() as a generator: transform values lazily
function* map(iterable, fn) {
for (const v of iterable) {
yield fn(v);
}
}
const doubled = map(countTo(4), function (x) { return x * 2; });
console.log("doubled:", [...doubled]);

Good habits
Tips:
- Call the generator function to get an iterator (it does not run fully up front).
- yield pauses; return ends the sequence.
- Once done, a generator is exhausted; call the function again for a fresh iterator.

Generator return value quiz
Quick check: What does a generator function return?

Recap
Recap: Define function*, yield values lazily, consume with for...of or next(), and compose tiny generator pipelines for simple transformations.

Frequently asked questions
Is the “Generator functions, yield, and small pipelines” lesson free?
Yes — the full text of “Generator functions, yield, and small pipelines” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Generator functions, yield, and small pipelines”?
Write generator functions with function*, use yield to produce values lazily, drive them with for...of or next(), and compose tiny pipelines. 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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Generator functions, yield, and small pipelines” 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
- Iterable protocol and for...of
- Generator functions, yield, and small pipelines
- Async iterators — for await...of and async generators