Check if a Variable is of Function Type using JavaScript
Last Updated :
03 Oct, 2024
A function in JavaScript is a 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.
javascript
let 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.
JavaScript
// Declare a variable and initialize it
// with anonymous function
let gfg = function () {/* A set of statements */ };
// Function to check a variable is of
// function type or not
function testing(x) {
if (x instanceof Function) {
console.log("Variable is of function type");
}
else {
console.log("Variable is not of function type");
}
}
// Function call
testing(gfg);
OutputVariable 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.
JavaScript
// Declare a variable and initialize it
// with anonymous function
let gfg = function () {/* A set of statements */ };
// Function to check a variable is of
// function type or not
function testing(x) {
if (typeof x === "function") {
console.log("Variable is of function type");
}
else {
console.log("Variable is not of function type");
}
}
// Function call
testing(gfg);
OutputVariable 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.
JavaScript
// Declare a variable and initialize it with an anonymous function
let gfg = function () {
// A set of statements
};
// Function to check if a variable is of function type or not
function testing(x) {
if (typeof x === 'function') {
console.log("Variable is of function type");
} else {
console.log("Variable is not of function type");
}
}
// Function call
testing(gfg);
OutputVariable is of function type
Similar Reads
Check if a variable is a string using JavaScript Checking if a variable is a string in JavaScript is a common task to ensure that the data type of a variable is what you expect. This is particularly important when handling user inputs or working with dynamic data, where type validation helps prevent errors and ensures reliable code execution.Below
3 min read
How to check if a Variable Is Not Null in JavaScript ? In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
4 min read
How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
JavaScript Check the existence of variable JavaScript has a built-in function to check whether a variable is defined/initialized or undefined. To do this, we will use the typeof operator. The typeof operator will return undefined if the variable is not initialized and the operator will return null if the variable is left blank intentionally.
2 min read
How to check the type of a variable or object in JavaScript ? In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type o
2 min read