How to check a variable is of function type using JavaScript ?

A function in JavaScript is the 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. A function can be assigned to a variable or passed to a method.

filter_none

edit
close

play_arrow

link
brightness_4
code

var gfg = function(){/* A set of statements */};

chevron_right


Here, an anonymous function is assigned to the variable named as ‘gfg’. There are various methods to check the variable is of function type or not. Some of them are discussed below:

  1. Using instanceof operator: The instanceof operator checks the type of an object at run time. It return a corresponding boolean value, i.e, either true or false to indicate if the object is of a particular type or not.

    Example: This example uses instanceof operator to check a variable is of function type or not.



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    // Declare a variable and initialize it
    // with anonymous function
    var gfg = function(){/* A set of statements */};
      
    // Function to check a variable is of
    // function type or not
    function testing(x) {
          
        if(x instanceof Function) {
            document.write("Variable is of function type");
        }
        else {
            document.write("Variable is not of function type");
        }
    }
      
    // Function call
    testing(gfg);
      
    </script>                      

    chevron_right

    
    

    Output:

    Variable is of function type
  2. Using Strict Equal (===) operator: In JavaScript, ‘===’ Operator is used to check whether two entities are of equal values as well as of equal type provides a boolean result. In this example, we use the ‘===’ operator. This operator, called the Strict Equal operator, checks if the operands are of the same type.

    Example: This example uses === operator to check a variable is of function type or not.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    // Declare a variable and initialize it
    // with anonymous function
    var gfg = function(){/* A set of statements */};
      
    // Function to check a variable is of
    // function type or not
    function testing(x)
    {   
        if (typeof x === "function") {
            document.write("Variable is of function type");
        }
        else {
            document.write("Variable is not of function type");
        }
    }
      
    // Function call
    testing(gfg);
      
    </script>                       

    chevron_right

    
    

    Output:

    Variable is of function type
  3. Using object.prototype.toString: This method uses object.prototype.toString. Every object has a toString() method, which is called implicitly when a value of String type is expected. If the toString() method is not overridden, by default it returns ‘[object type]’ where ‘type’ is the object type.

    Example: This example uses object.prototype.toString operator to check a variable is of function type or not.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    // Declare a variable and initialize it
    // with anonymous function
    var gfg = function(){/* A set of statements */};
      
    // Function to check a variable is of
    // function type or not
    function testing(x)
    {   
        if (Object.prototype.toString.call(x) == '[object Function]')
        {
            document.write("Variable is of function type");
               
        }
        else {
            document.write("Variable is not of function type");
        }
    }
      
    // Function call
    testing(gfg);
      
    </script>     

    chevron_right

    
    

    Output:

    Variable is of function type


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.