Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 27 Mar, 2020
Geek Week

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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :