Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to Check a Function is a Generator Function or not using...

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.




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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments