Check if a variable is of function type or not

A JavaScript function is a block of code designed to perform a particular task. It is executed when it is invoked(when something calls it). A function can be either a named or an anonymous one. This article talks about how to go about checking whether a variable is of 'Function' type or not. Before we understand the different methods of implementing this and also why anyone would want to assign a function to a variable let's look at how named and anonymous functions are declared.

Table of Contents


Function declaration types

Named Function declaration

This function has a named identifier associated with it which can be used to invoke the function
 function functionName(parameter1, paramter2) {//code}

Anonymous Function declaration

It is a function that is declared without any named identifier to refer to it.
var anon = function(){//code }


Advantage of assigning a function to a variable

Assigning a function to a variable allows us to pass this variable as a parameter to another function. This is particularly useful in scenarios that require runtime flexibility. You would mainly use such functions to run a load of code in response to an event firing For example, a button being clicked using an event handler.
myButton.onclick = function() {
 //response actions
}


Code

Using instanceof operator

The instanceof operator is used to check the type of objects at run time. This operator returns a Boolean value(true or false). In the example below, an IF statement is used to check if the type of parameter passed to checkFunction() is of Function type or not.

//javascript check if function-Using instanceof operator
<script>

// Declare a variable and initialize it // Declare a variable and initialize it with an anonymous function
var exampleVar = function(){/* A set of statements */};

// to check a variable is of function type or not
function checkFunction(x)
{

    if(x instanceof Function) {
        document.write("Variable is of function type");
    }
    else {
        document.write("Variable is not of function type");
    }
}

// Function call
checkFunction(exampleVar);

</script>


Using Strict Equality comparison (===) along with typeof operator

In JavaScript, strict equality comparison (===) Operator is used to check whether two entities are of not only equal values but also of equal type. The typeof operator returns a string which indicates the type of the unevaluated operand. Both of these operators provide a Boolean result. This result can be compared using the IF statement to check if the object type is "Function'.
//javascript check if function-Using Strict Equality comparison (===) along with typeof operator
<script>

// Declare a variable and initialize it with an anonymous function
var exampleVar = function(){/* A set of statements */};

// to check a variable is of function type or not
function checkFunction(x)
{
    if (typeof x === "function") {
        document.write("Variable is of function type");
    }
    else {
        document.write("Variable is not of function type");
    }
}

// Function call
checkFunction(exampleVar);

</script>


Using object.prototype.toString

This method uses object.prototype.toString. Every object has a toString() method, which returns ‘[object type]’ where ‘type’ is the object type. An IF statement can be used to compare if the returned value is of the type 'Function'.

//javascript check if function-Using object.prototype.toString
<script>

// Declare a variable and initialize it with an anonymous function
var exampleVar = function(){/* A set of statements */};

// to check a variable is of function type or not
function checkFunction(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
checkFunction(exampleVar);

</script>


Caveats

In Chrome typeof(obj) === 'function' appears to be the fastest; however, in Firefox obj instanceof Function is performs relatively better. Related Concepts

Other Related Concepts

Polymorphism in JavaScript2 min read

<Intro>

Difference between typeof and instanceof operators in JavaScript2 min read

<Intro>

    Try a Top Quality Javascript Developer for 7 Days.
    Pay Only If Satisfied.