How to Check a Function is a Generator Function or not using JavaScript ?

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.