Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
JavaScript Program to Check If a Variable is of Function Type
In this example, you will learn to write a JavaScript program that will check if a variable is of function type.
To understand this example, you should have the knowledge of the following JavaScript programming topics:
Example 1: Using instanceof Operator
// program to check if a variable is of function type
function testVariable(variable) {
if(variable instanceof Function) {
console.log('The variable is of function type');
}
else {
console.log('The variable is not of function type');
}
}
const count = true;
const x = function() {
console.log('hello')
};
testVariable(count);
testVariable(x);
Output
The variable is not of function type The variable is of function type
In the above program, the instanceof operator is used to check the type of variable.
Example 2: Using typeof Operator
// program to check if a variable is of function type
function testVariable(variable) {
if(typeof variable === 'function') {
console.log('The variable is of function type');
}
else {
console.log('The variable is not of function type');
}
}
const count = true;
const x = function() {
console.log('hello')
};
testVariable(count);
testVariable(x);
Output
The variable is not of function type The variable is of function type
In the above program, the typeof operator is used with strict equal to === operator to check the type of variable.
The typeof operator gives the variable data type. === checks if the variable is equal in terms of value as well as the data type.
Example 3: Using Object.prototype.toString.call() Method
// program to check if a variable is of function type
function testVariable(variable) {
if(Object.prototype.toString.call(variable) == '[object Function]') {
console.log('The variable is of function type');
}
else {
console.log('The variable is not of function type');
}
}
const count = true;
const x = function() {
console.log('hello')
};
testVariable(count);
testVariable(x);
Output
The variable is not of function type The variable is of function type
The Object.prototype.toString.call() method returns a string that specifies the object type.
Also Read:
Did you find this article helpful?