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 Check if a Variable is of Function Type using JavaScript A function in JavaScript is a set of statements used to perform a specific task. A function can be either a named one or an anonymous one. The set of statements inside a function is executed when the function is invoked or called. javascriptlet gfg = function(){/* A set of statements */};Here, an an 3 min read How to check if a string is html or not using JavaScript? The task is to validate whether the given string is valid HTML or not using JavaScript. we're going to discuss a few techniques. Approach Get the HTML string into a variable.Create a RegExp which checks for the validation.RegExp should follow the rules of creating an HTML document. Example 1: In thi 2 min read How to Check if the Clicked Element is a div or not in JavaScript? To check if the clicked element is a div in JavaScript, use the click event listener and check the tag name of the target element.Below are the approaches to check if the clicked element is a div or not:Table of ContentUsing element.tagName PropertyUsing instanceOf OperatorApproach 1: Using element. 2 min read How to Check if a Given Number is Fibonacci Number in JavaScript ? The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. Checking for Fibonacci numbers involves verifying whether a given number appears in this sequence. The first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and 4 min read What to understand the Generator function in JavaScript ? Generators generate value on the fly which means whenever there is a need for that value then only it will be generated. It means the value is generated but not stored in memory so it takes less time to execute. It uses asterick (*) symbol after the keyword function i.e. function* to tell javaScript 4 min read Like