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.
var gfg = function(){/* A set of statements */}; |
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:
- 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.
<script>ÂÂ// Declare a variable and initialize it// with anonymous functionvargfg =function(){/* A set of statements */};ÂÂ// Function to check a variable is of// function type or notfunctiontesting(x) {Â Â Â Â ÂÂÂ Â Â Âif(xinstanceofFunction) {Â Â Â Â Â Â Â Âdocument.write("Variable is of function type");Â Â Â Â}Â Â Â Âelse{Â Â Â Â Â Â Â Âdocument.write("Variable is not of function type");Â Â Â Â}}ÂÂ// Function calltesting(gfg);ÂÂ</script>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂOutput:
Variable is of function type
- 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.
<script>ÂÂ// Declare a variable and initialize it// with anonymous functionvargfg =function(){/* A set of statements */};ÂÂ// Function to check a variable is of// function type or notfunctiontesting(x){Â Â ÂÂ Â Â Âif(typeofx ==="function") {Â Â Â Â Â Â Â Âdocument.write("Variable is of function type");Â Â Â Â}Â Â Â Âelse{Â Â Â Â Â Â Â Âdocument.write("Variable is not of function type");Â Â Â Â}}ÂÂ// Function calltesting(gfg);ÂÂ</script>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂOutput:
Variable is of function type
- 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.
<script>ÂÂ// Declare a variable and initialize it// with anonymous functionvargfg =function(){/* A set of statements */};ÂÂ// Function to check a variable is of// function type or notfunctiontesting(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 calltesting(gfg);ÂÂ</script>Â Â Â Â ÂOutput:
Variable is of function type
