Given an HTML document containing some JavaScript function and the task is to check whether the given function is generator function or not with the help of JavaScript. There are two examples to solve this problem which are discussed below:
Example 1: In this example, we will use functionName.constructor.name property. It functionName.constructor.name property value is equal to the ‘GeneratorFunction’ then the given function will be the generator function.
<!DOCTYPE HTML> <html> <head> <title> Check whether a given function is a generator function or not in JavaScript </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p style= "font-size: 19px; font-weight: bold;"> Click on button to check whether a function is generator or not? </p> <button onClick="GFG_Fun()"> click here </button> <p id="GFG" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> var gfg = document.getElementById('GFG'); function * generatorFunction() { yield 10; yield 30; } function isGenerator(fn) { return fn.constructor.name === 'GeneratorFunction'; } function GFG_Fun() { gfg.innerHTML = isGenerator(generatorFunction); } </script> </body> </html> |
Output:

Example 2: In this example, we will use instanceof operator. First define the generator function then check the value returned by instanceof operator is equal to the generator function or not.
<!DOCTYPE HTML> <html> <head> <title> Check whether a given function is a generator function or not in JavaScript </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p style= "font-size: 19px; font-weight: bold;"> Click on button to check whether a function is generator or not? </p> <button onClick="GFG_Fun()"> click here </button> <p id="GFG" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> var gfg = document.getElementById('GFG'); function *genFun() { yield 10; yield 30; } function GFG_Fun() { var GeneratorFunction = (function*(){ yield undefined; }).constructor; gfg.innerHTML = genFun instanceof GeneratorFunction; } </script> </body> </html> |
Output:

Recommended Posts:
- How to create linear-gradient color generator using HTML, CSS and JavaScript ?
- JavaScript | Generator
- JavaScript | Generator.prototype.throw() Method
- JavaScript | Generator.prototype.return() Method
- JavaScript | Generator.prototype.next() Method
- Creating Socket.IO Server using Express Generator
- QR Code Generator using HTML, CSS and jQuery
- PHP | FPDF-PDF Generator
- Node.js | crypto.createDiffieHellman(primeLength, generator) Method
- Node.js | crypto.createDiffieHellman(prime, primeEncoding, generator, generatorEncoding) Method
- How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
- Check a number is Prime or not using JavaScript
- How to check if a string is html or not using JavaScript?
- How to check a date is valid or not using JavaScript ?
- How to check input file is empty or not using JavaScript/jQuery ?
- How to check the input date is equal to today's date or not using JavaScript ?
- How to Check Mentioned File Exists or not using JavaScript/jQuery ?
- How to check whether the background image is loaded or not using JavaScript ?
- What is the !! (not not) operator in JavaScript?
- How to check if the clicked element is a div or not in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


