JavaScript
Programming
Functions
Variable detection
Code Tips

How do I detect whether a variable is a function?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In JavaScript, the standard way to check whether a value is a function is typeof value === "function". That answer is simple, but it helps to understand what it actually detects and how it differs from broader ideas such as “callable” or “method.”

The Standard JavaScript Check

Functions are first-class values in JavaScript, so you can store them in variables, pass them around, and test them at runtime.

javascript
1function greet() {
2  return "hello";
3}
4
5const value = greet;
6const other = 42;
7
8console.log(typeof value === "function");
9console.log(typeof other === "function");

Output:

text
true
false

For normal JavaScript code, this is the correct and idiomatic check.

Why typeof Is Better Than Other Tricks

Beginners often try checks such as these:

  • 'value instanceof Function'
  • 'value && value.call'
  • comparing constructors manually

Those approaches are either less direct or easier to misuse. typeof is built for exactly this purpose.

javascript
const fn = () => "ok";
console.log(typeof fn);

If the result is "function", the runtime considers that value a function object.

Functions Versus Methods

A method is just a function stored on an object.

javascript
1const user = {
2  sayHello() {
3    return "hello";
4  },
5};
6
7console.log(typeof user.sayHello === "function");

So if you are checking whether an object property can be invoked as a method, the same rule applies.

A Safe Invocation Pattern

If the real goal is “call this only if it is a function,” combine the check with invocation.

javascript
1function runIfFunction(value, ...args) {
2  if (typeof value === "function") {
3    return value(...args);
4  }
5  return undefined;
6}
7
8console.log(runIfFunction((a, b) => a + b, 2, 3));
9console.log(runIfFunction("not a function", 2, 3));

This is common in callback-based APIs where an option may or may not be present.

Edge Cases Worth Knowing

In JavaScript, classes are also functions under the hood.

javascript
class Person {}
console.log(typeof Person === "function");

That prints true. This is correct according to the language, but it can surprise developers who mentally separate “class” and “function.”

Another detail is cross-realm behavior. Values created in another browser frame or window can make instanceof Function unreliable, while typeof value === "function" is still the safer check.

What This Check Does Not Mean

Checking for a function does not tell you whether calling it is safe, whether it expects arguments, or whether it will throw. It only tells you that the value is a function object.

That distinction matters when building plugin systems or optional callback APIs. Type checking is only the first layer. You may still need contract checks or defensive error handling.

Optional Chaining and Function Calls

If you are calling an optional method on an object, optional chaining can help with property access, but it does not replace function detection in every case.

javascript
1const plugin = {
2  onStart() {
3    console.log("started");
4  },
5};
6
7plugin.onStart?.();

This works when the property is missing or is a function. If the property exists but is not callable, you still have a type problem. In that case, a typeof check is clearer.

Common Pitfalls

The most common mistake is checking truthiness instead of function type. A non-empty string or object is truthy, but it is not callable.

Another mistake is using instanceof Function as the default solution. It works in many cases, but typeof is simpler and more robust for normal JavaScript runtime checks.

Developers also sometimes forget that classes report as functions. If your code should allow ordinary functions but reject class constructors, you need a stricter application-specific rule than typeof alone.

Summary

  • In JavaScript, the normal check is typeof value === "function".
  • The same rule works for standalone functions and object methods.
  • 'typeof is usually better than instanceof Function for simple runtime checks.'
  • A function check only tells you the value is callable, not that calling it is correct for your API.
  • Classes also report as functions, so stricter validation may be needed in special cases.

Course illustration
Course illustration

All Rights Reserved.