Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I check if a JavaScript variable is function type?
In this tutorial, we will learn different approaches to checking if a JavaScript variable is of function type or not. In JavaScript, functions contain blocks of code that improve code reusability.
There are mainly two ways to declare functions: named functions and anonymous functions. When declaring anonymous functions, we need to store them in variables to access and call them later.
Here's how to declare an anonymous function and store it in a variable:
let tutorialspoint = function() {
// code for the function
}
As you can see, the 'tutorialspoint' variable contains the function, and sometimes programmers need to check if it is a function type or not.
To solve this problem, we can use the following methods:
- Using the typeof operator
- Using the instanceof operator
- Using the Object.prototype.toString method
Using the typeof Operator
The typeof operator determines the data type of any variable and returns the data type as a string. We can compare the returned value with the string 'function' using the strict equality operator (===).
Syntax
typeof operand typeof(operand)
Parameters
- operand ? Any variable or object to check if it's a function type.
Return Value ? The data type of 'operand' as a string.
Example 1
The following example demonstrates using the typeof operator to check if a variable is a function:
<html>
<head>
<title>Check type of variable is of function</title>
</head>
<body>
<h2>typeof Operator: Check if a variable is of function type</h2>
<div id="result"></div>
<script type="text/javascript">
let a = 10;
let b = 20;
var tutorialsPoint = function () {
return a + b;
};
// function to check type of variable is function or not
function checkTypeOfVar() {
if (typeof tutorialsPoint === 'function') {
document.write("<br>The type of <i>tutorialsPoint</i> variable is function.");
} else {
document.write("The type of <i>tutorialsPoint</i> variable is not a function.");
}
}
document.getElementById("result").innerHTML = "var tutorialsPoint = " + tutorialsPoint;
// call the function
checkTypeOfVar();
</script>
</body>
</html>
var tutorialsPoint = function () { return a + b; }
The type of tutorialsPoint variable is function.
Using the instanceof Operator
The instanceof operator determines whether a variable is of a given type. It takes two operands: a variable and a data type to compare against. It returns a Boolean value - true if the variable matches the given type, false otherwise.
Syntax
variable instanceof Function
Example 2
In the following example, we check the function type using the instanceof operator:
<html>
<head>
<title>Check type of variable is of function</title>
</head>
<body>
<h2><i>instanceof</i> Operator: check if a variable is of function type</h2>
<div id="result"></div>
<script type="text/javascript">
let a = 10;
let b = 20;
var multiply = function () {
return a * b;
};
// function to check type of variable is function or not
function checkTypeOfVar() {
let isTypeOfFunction = multiply instanceof Function;
if (isTypeOfFunction === true) {
document.write("<br>The type of <i>multiply</i> variable is <b>function</b>.");
} else {
document.write("<br>The type of multiply variable is not a function.");
}
}
document.getElementById("result").innerHTML = "var multiply = " + multiply;
// call the function
checkTypeOfVar();
</script>
</body>
</html>
var multiply = function () { return a * b; }
The type of multiply variable is function.
Using the Object.prototype.toString Method
The Object.prototype.toString method is called automatically when an object needs to return a string representation. For functions, it returns '[object Function]'. This method provides the most reliable type checking.
Syntax
Object.prototype.toString.call(variable) === '[object Function]'
Parameters
- variable ? The variable to check if it's a function type.
Example 3
In the following example, we get the type of the variable using the Object.prototype.toString method:
<html>
<head>
<title>Check type of variable is of function</title>
</head>
<body>
<h2><i>Object.prototype.toString()</i> Method</h2>
<div id="result"></div>
<script type="text/javascript">
let a = 10;
let b = 20;
var subtract = function () {
return a - b;
};
// function to check type of variable is function or not
function checkTypeOfVar() {
// get the type of subtract variable
let typeOfVar = Object.prototype.toString.call(subtract);
// compare typeOfVar with the function type
if (typeOfVar === '[object Function]') {
document.write("<br>The type of <i>subtract</i> variable is <b>function</b>.");
} else {
document.write("<br>The type of subtract variable is not a function.");
}
}
document.getElementById("result").innerHTML = "var subtract = " + subtract;
// call the function
checkTypeOfVar();
</script>
</body>
</html>
var subtract = function () { return a - b; }
The type of subtract variable is function.
Comparison
| Method | Syntax Simplicity | Reliability | Best Use Case |
|---|---|---|---|
typeof |
Simple | Good | Basic function checking |
instanceof |
Simple | Good | Object-oriented contexts |
Object.prototype.toString |
Complex | Most reliable | Cross-frame compatibility |
Conclusion
We've explored three methods to check if a variable is a function type. The typeof operator is the most commonly used due to its simplicity and reliability. Use Object.prototype.toString when you need the most robust type checking across different execution contexts.
