How to Check a Function is a Generator Function or not using JavaScript ? Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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. html <!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. html <!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: Comment More infoAdvertise with us Next Article How to Check a Function is a Generator Function or not using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc Similar Reads How to get the function name from within that function using JavaScript ? Given a function and the task is to get the name of the function from inside the function using JavaScript. There are basically two methods to get the function name from within that function in JavaScript. These are: Using String substr() MethodUsing Function prototype name propertyGet the Function 2 min read How to check a function is defined in JavaScript ? In this article, we will check whether a function is defined or not in JavaScript. The JavaScript typeof operator is used to check whether the function is defined or not. JavaScript typeof OperatorThe typeof operator is used to find the type of a JavaScript variable. This operator returns the type o 2 min read How to Check if an element is a child of a parent using JavaScript? In this article, we are going to see the methods by which we can Check if an element is a child of a parent using JavaScript. These are the following methods: Table of Content Using the Node.contains() methodLooping through the parents of the given childUsing the hasChildNodes() methodMethod 1: Usin 5 min read Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin 3 min read How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if 2 min read Like