Back to JavaScript
2026-01-066 min read

JavaScript Program to Check If a Variable is of Function Type

Learn JavaScript Program to Check If a Variable is of Function Type step by step with clear examples and exercises.

Why This Matters

Understanding how to check if a variable is of function type in JavaScript is crucial for several reasons:

  1. Avoiding runtime errors: If you try to call a non-function variable as if it were a function, you'll encounter runtime errors that can be challenging to debug.
  2. Improving code readability and maintainability: By checking the type of variables before using them, you ensure that your code is more robust and easier for others to understand and work with.
  3. Preparing for interviews and real-world programming tasks: Knowing how to check for function types can be a valuable skill during job interviews or when working on complex projects.

Prerequisites

Before diving into the core concept, it's essential that you have a solid understanding of the following JavaScript topics:

  1. Variables and data types: Familiarize yourself with the different data types in JavaScript, including primitive types (number, string, boolean, null, undefined) and complex types (objects, arrays).
  2. Operators: Learn about arithmetic operators, comparison operators, logical operators, and assignment operators.
  3. Functions and function declarations: Understand how to declare functions using both function declarations and arrow functions.
  4. The typeof operator: Learn how the typeof operator works in JavaScript and its limitations when dealing with complex types like objects and arrow functions.

Core Concept

To check if a variable is of function type in JavaScript, we can use two approaches:

  1. Using the instanceof operator
  2. Using the typeof operator
  3. Using the Function.prototype.toString.call() method (for arrow functions)
  4. Checking for function properties directly (for objects with methods)

Using the instanceof Operator

The instanceof operator checks whether an object is an instance of a specific constructor. In our case, we can use it to check if a variable is a function:

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); // Output: The variable is not of function type
testVariable(x); // Output: The variable is of function type

Using the typeof Operator

Using the typeof operator to check for a function type in JavaScript can be misleading, as it returns 'function' for both function declarations and arrow functions. However, it's still useful for checking traditional function declarations:

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); // Output: The variable is not of function type
testVariable(x); // Output: The variable is of function type

Using the Function.prototype.toString.call() method (for arrow functions)

To differentiate between traditional function declarations and arrow functions using the typeof operator, we can use the Function.prototype.toString.call() method:

const isArrowFunction = (variable) => {
return /^\s*function\(\)\s*{.*\}\s*$/.test(Function.prototype.toString.call(variable));
};

function testVariable(variable) {
if (isArrowFunction(variable)) {
console.log('The variable is an arrow function');
} else if (typeof variable === 'function') {
console.log('The variable is a traditional function declaration');
} else {
console.log('The variable is not of function type');
}
}
const count = true;
const x = function() { console.log('hello') };
const y = () => { console.log('world') };
testVariable(count); // Output: The variable is not of function type
testVariable(x); // Output: The variable is a traditional function declaration
testVariable(y); // Output: The variable is an arrow function

Checking for function properties directly (for objects with methods)

To check if an object has any properties that are functions, you can use the following approach:

const hasFunctionProperties = (obj) => {
for (let key in obj) {
if (typeof obj[key] === 'function') {
return true;
}
}
return false;
};

const obj = {
sayHello() { console.log('hello') },
sayWorld: () => { console.log('world') }
};

console.log(hasFunctionProperties(obj)); // Output: true

Worked Example

Let's consider a scenario where we have an array of variables and want to find the ones that are functions:

const variables = [true, 123, 'hello', () => console.log('world'), function() { console.log('function') }];

variables.forEach(variable => {
if (typeof variable === 'function') {
console.log(`Variable ${variables.indexOf(variable)} is of function type`);
}
});

In this example, we create an array variables containing several values, including a function. We then use the forEach loop to iterate through the array and check if each variable is a function using the typeof operator. If it is, we log the index of the variable in the array along with "is of function type".

Common Mistakes

  1. Forgetting to check for both function declarations and arrow functions:
const testVariable = (variable) => {
if (typeof variable === 'function') {
console.log('The variable is of function type');
} else {
console.log('The variable is not of function type');
}
};

// This will cause an error because testVariable itself is a function but not declared as such:
testVariable(testVariable); // TypeError: testVariable is not a function
  1. Assuming that objects are functions due to the instanceof Function check:
const obj = {
sayHello() { console.log('hello') }
};

if (obj instanceof Function) {
console.log('The variable is of function type'); // This will output false, but it's a common mistake to expect true
}
  1. Not considering the limitations of the typeof operator when dealing with arrow functions:
const testVariable = (variable) => {
if (typeof variable === 'function') {
console.log('The variable is of function type');
} else {
console.log('The variable is not of function type');
}
};

// This will output "The variable is of function type", but it's a common mistake to assume that the `typeof` operator can differentiate between arrow functions and traditional function declarations
const y = () => { console.log('world') };
testVariable(y);

Practice Questions

  1. Write a JavaScript function that takes an array of variables and returns a new array containing only the ones that are functions.
  2. Given the following code snippet, what will be printed to the console when running it?
const x = () => { console.log('hello') };
const y = 123;
function testVariable(variable) {
if (typeof variable === 'function') {
console.log(`Variable is of function type`);
} else {
console.log(`Variable is not of function type`);
}
}
testVariable(x);
testVariable(y);

FAQ

  1. Why can't I use typeof to check for arrow functions?
  • Arrow functions are objects in JavaScript, and the typeof operator returns 'function' for both function declarations and arrow functions. However, you can differentiate between them using the Function.prototype.toString.call() method:
const isArrowFunction = (variable) => {
return /^\s*function\(\)\s*{.*\}\s*$/.test(Function.prototype.toString.call(variable));
};
  1. Why does the instanceof Function check not work for objects that have a method with a function type?
  • The instanceof operator checks if an object is an instance of a specific constructor, but in JavaScript, objects are not considered instances of the Function constructor even when they contain methods that are functions. To check if an object has any properties that are functions, you can use the following approach:
const hasFunctionProperties = (obj) => {
for (let key in obj) {
if (typeof obj[key] === 'function') {
return true;
}
}
return false;
};
  1. Why does the instanceof Function check work for traditional function declarations but not arrow functions?
  • Traditional function declarations are created in the current scope, and their constructors are the global Function constructor. Arrow functions, on the other hand, are created as properties of an outer object (usually the global object) and do not have a direct relationship with the Function constructor.
  1. What is the difference between function declarations and arrow functions?
  • Function declarations are defined using the function keyword, while arrow functions use the => syntax. Arrow functions have a lexical this, do not create their own arguments object, and cannot be used as constructors (unlike traditional function declarations). Additionally, arrow functions may behave differently when dealing with this, depending on how they are called.
JavaScript Program to Check If a Variable is of Function Type | JavaScript | XQA Learn