Javascript Generators (ES6/ES2015)


  • Basic example (demonstrated with iteration)
  • Properties
  • Methods
  • Using “return” with Generators
  • Passing arguments
  • Generators with ES6katas
  • Summary

Basic example (demonstrated with iteration) link


None listed on MDN

Methods a value yielded by the yield expression.

Generator.prototype.return()Returns the given value and finishes the generator.

Generator.prototype.throw()Throws an error to a generator (also finishes the generator, unless caught from within that generator).

Using “return” with Generators link

Passing arguments link

Generators with ES6katas

Least intimidating possible way to start exploring:


Generators return an object like this { value: 10, done: false } every time they encounter a yield statement. It returns done: true AFTER the last yield, OR when a return statement is returned.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

A generator function returns a Generator object which conforms to both the iterable protocol and the iterator protocol. — MDN


This is intended firstly as notes/future reference for myself. Secondarily for others, and lastly as a tutorial. For these reasons I’ve decided upon the following conventions:

  • Absolute minimization of text (particularly non-code text)
  • Skip an introduction — if an overview is needed in one of my posts always skip down to the summary

While a wider audience is not my main aim, I also hope this explanation provides helpful insight to those who do stumble across this. I maintain these conventions in all posts/videos I create.