function*

function – Declares a generator.

Description

function ([mixed param, …])

Parameters

Name Description Type Default Optional
param, … Variables to pass into the generator. mixed Yes

Changelog

Version Description
ES 6 Introduced.

Examples

Example #1 – function* example
function* keyGen () { var key = 0; while (true) { yield key++; } } var key = keyGen(); console.log(key.next()); // Object {value: 0, done: false} console.log(key.next()); // Object {value: 1, done: false} console.log(key.next()); // Object {value: 2, done: false} console.log(key.next().value); // 3
Example #2 – function* example
function* range (start, end) { while (start < end) { yield start; start += 1; } } for (var i of range(0, 5)) { console.log(i); }

See also

  • yield

External references