Broad Network

ECMAScript Generator Basics

Generator in ECMAScript 2015 – Part 1

ECMAScript 6

Foreword: In this part of the series, I give you the basics of ECMAScript generator.

By: Chrysanthus Date Published: 21 Jul 2016


In this part of the series, I give you the basics of ECMAScript generator. A collection is an array or a set or a map. In ECMAScript, generator is a way of creating and iterating a custom collection.

Imagine a class of 5 students who wrote a test. 3 students had marks; one student was absent and the result of one was cancelled. The marks (result) may be written as follows:


This is a collection.

Creating a Custom Collection
You can create a custom collection in two ways. You can create the above collection like,

    function* myCollection()
            yield 56;
            yield 70;
            yield 'absent';
            yield 60;
            yield 'cancelled';

or like,

    function* myCollection()
            yield* [56, 70, 'absent', 60, 'cancelled'];

Note the use of the asterisk. myCollection is a name of your choice, while function is a reserved word. yield is also a reserved word.

Iterating through the Collection
The following for-loop iterates through the collection:

    for (item of myCollection())

Note the use of “item of” where item is a variable name of your choice.

That is it for this part of the series.


Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course


Become the Writer's Follower
Send the Writer a Message