# Generators in JavaScript

There are special functions with which you can create your own iterators. Such functions are called generators. The names of such functions must begin with an asterisk:

`function *func() { }`

Inside generators, the `yield` keyword is used to indicate what the iterator should return on the next call. For example, let's make the first call return `1`, the second return `2`, and the third return `3`:

`function *func() { yield 1; yield 2; yield 3; }`

The generator returns an iterator as its result:

`let iter = func();`

Let's test our iterator:

`console.log(iter.next()); // {value: 1, done: false} console.log(iter.next()); // {value: 2, done: false} console.log(iter.next()); // {value: 3, done: false} console.log(iter.next()); // {value: undefined, done: true}`

Create an iterator, each call of which will return numbers from `5` to `1`.

←→
↑
en ru