Skip to content
Related Articles
Open in App
Not now

Related Articles

How to check a function is defined in JavaScript ?

Improve Article
Save Article
  • Last Updated : 26 Dec, 2022
Improve Article
Save Article

In this article, the job is to identify whether a function is defined or not. The JavaScript typeof operator is used to solve the problem described below: 

Javascript typeof Operator: This operator can be used to find the type of a JavaScript variable. This operator returns the type of a variable or an expression: 

Syntax: 

typeof var

Parameter: It contains a single value var which is a Javascript variable. 

Return value: It returns the type of a variable or an expression: 

Example 1: This example checks the type of the function, If it is a function then it is defined otherwise not defined by using typeof operator

html




<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>How to check a function is defined</h3>
  
    <p id="GFG_UP" style="font-size: 16px; 
                          font-weight: bold;">
    </p>
  
    <button onclick="gfg_Run()">
        Click here
    </button>
  
    <p id="GFG_DOWN" style="color:green; 
                            font-size: 20px; 
                            font-weight: bold;">
    </p>
  
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
          
        el_up.innerHTML =
            "Function named 'fun' which is not defined";
              
        function gfg_Run() {
            var defined = 'Not defined';
            if(typeof fun == 'function') {
                defined = "Defined";
            }
            el_down.innerHTML = defined;
        }        
    </script>
</body>

Output:

 

Example 2: This example checks the type of the function, If it is a function then it is defined otherwise not defined by using typeof operator by creating a function. 

html




<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>How to check a function is defined</h3>
  
    <p id="GFG_UP" style="font-size: 16px; 
                          font-weight: bold;">
    </p>
  
    <button onclick="gfg_Run()">
        Click here
    </button>
  
    <p id="GFG_DOWN" style="color:green; 
                            font-size: 20px; 
                            font-weight: bold;">
    </p>
  
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
          
        el_up.innerHTML = "Function named 'fun' which is defined";
          
        function isFunction(possibleFunction) {
            return typeof(possibleFunction) === typeof(Function);
        }
          
        function fun() {
              
        }
          
        function gfg_Run() {
            var defined = 'Not defined';
            if(isFunction(fun)) {
                defined = "Defined";
            }
            el_down.innerHTML = defined;
        }        
    </script>
</body>

Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!