JavaScript Generator Reference Last Updated : 18 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report JavaScript Generator is used to allow us to define an iterative algorithm by writing a single function whose execution is not continuous.Generator Constructor & Generator ObjectsGenerator() Constructor: A generator constructor is defined as a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return.Generator Object: Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for of” loop.Syntax:// An example of the generator functionfunction* gen(){ yield 1; yield 2; ... ...}Example: Below is an example code to print infinite series of natural numbers using a simple generator. JavaScript function* nextNatural() { let naturalNumber = 1; // Infinite Generation while (true) { yield naturalNumber++; } } // Calling the Generate Function let gen = nextNatural(); // Loop to print the first // 10 Generated number for (let i = 0; i < 10; i++) { // Generating Next Number console.log(gen.next().value) } Output:1 2345678910The complete list of JavaScript Generators are listed below.JavaScript Generator Constructor: In JavaScript, a constructor gets called when an object is created using the new keyword.ConstructorDescriptionGeneratorIt is not available globally. Instances of Generator returned from generator functions.JavaScript Generator Properties: A JavaScript property is a member of an object that associates a key with a value.Instance Property: An instance property is a property that has a new copy for every new instance of the class.Instance PropertiesDescriptionconstructorReturn the generator constructor function for the object.JavaScript Generator Methods: JavaScript methods are actions that can be performed on objects.Instance Method: If the method is called on an instance of a generator then it is called an instance method.Instance MethodsDescriptionnext()Return an object with two properties done and value.return()Return the given value and finishes the generator.throw()The execution of a generator by throwing an error into it. Comment More infoAdvertise with us Next Article JavaScript Generator throw() Method K kartik Follow Improve Article Tags : JavaScript Web Technologies Similar Reads JavaScript Generator return() Method JavaScript Generator.prototype.return() method is an inbuilt method in JavaScript that is used to return the given value and finishes the generator. Syntax: gen.return( value ); Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter holds the 2 min read JavaScript Generator next() Method JavaScript Generator.prototype.next() method is an inbuilt method in JavaScript that is used to return an object with two properties done and value. Syntax: gen.next( value ); Parameters: This function accepts a single parameter as mentioned above and described below: value: This parameter holds the 2 min read JavaScript Generator throw() Method JavaScript Generator.prototype.throw() method is an inbuilt method in JavaScript that is used to resume the execution of a generator by throwing an error into it. Syntax: gen.throw(exception); Parameters: This function accepts a single parameter as mentioned above and described below: exception: Thi 2 min read JavaScript Generator constructor Property JavaScript Generator constructor property is used to return the Generator constructor function for the object. The function which is returned by this property is just the reference to this function, not a Generator containing the functionâs name. The JavaScript number constructor, string constructor 1 min read JavaScript Function Generator A generator function is a special type of function that can pause its execution at any point and resume later. They are defined using the function* syntax and use the yield keyword to pause execution and return a value.Syntaxfunction* generatorFunction() { // Code that can yield multiple values}Java 4 min read JavaScript Generator() Constructor In JavaScript, there is no particular Generator() constructor but instead, we can use generator function declaration to construct a Generator object which is iterable as the Generator object is a subclass of the Iterable class. Generators are usually used to create functions that can be exited and r 1 min read Like