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.
Output:
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.
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.
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.
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.
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.
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.
- '
typeofis usually better thaninstanceof Functionfor 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.

