DEV Community

Discussion on: Practical Explanation Of Javascript Iterators(with a demo app)

Collapse
 
kepta profile image
Kushan Joshi • Edited

I like your real demo, but I think this article failed to mention an important point.

Iterators are supposed to be used in conjunction with iterables.

In your example

// Create an array
let names = ['wale', 'ali', 'john', 'bubu'];
 // pass the array into the Iterator function
let name = Iterator(names);

I understand that you want to show how iterators work, but iterators are not functions.
You can even use the name array directly since it is an iterable.

// Create an array
let names = ['wale', 'ali', 'john', 'bubu'];
let name = names[Symbol.iterator]();

name.next(); // {value: "wale", done: false}
name.next(); // {value: "ali", done: false}

I wrote an in depth article on this very topic, please check it out.