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 usual way to detect whether a value is callable is typeof value === "function". That answer is short, but the topic becomes more interesting once you start dealing with callbacks, class constructors, cross-frame values, and objects that only look function-like.
In most codebases, the important question is not "is this technically a function object" in the abstract. It is "is it safe and reasonable for me to call this value right now".
The Standard Check
For normal JavaScript code, typeof is the right starting point.
That works for:
- normal functions
- arrow functions
- async functions
- generator functions
- class constructors, because JavaScript classes are functions internally
Example:
All of those print function.
Why typeof Is Usually Better Than instanceof
You may also see this pattern:
It often works, but it is less reliable across realms such as iframes or other JavaScript execution contexts. A function created in another realm may not share the same Function constructor reference.
For that reason, typeof value === "function" is generally the better default.
That helper is simple, readable, and correct for most real-world needs.
Practical Callback Guarding
The most common use case is guarding optional callbacks.
This avoids errors like TypeError: onDone is not a function when callers omit the callback or pass the wrong type.
If you control the API, a slightly stricter approach is often better: throw early instead of silently skipping invalid input.
When a Value Looks Callable but Is Not
Some values are easy to mistake for functions:
- objects that contain a
callmethod - objects returned by libraries that represent deferred work
- strings containing function names
- methods accessed incorrectly and therefore evaluated to
undefined
For example:
The object itself is not a function. Only its call property is.
That distinction matters when passing callbacks around. If you mean to pass a method, pass the method reference, not the whole object.
A Note on Host Objects and Old Advice
Older JavaScript advice sometimes recommends Object.prototype.toString.call(value) to detect function values. That can still work in niche cases, but it is usually more verbose than necessary.
In modern code, prefer typeof unless you have a very specific reason to inspect the tag string.
Common Pitfalls
The biggest mistake is using instanceof Function as the default check. It is fine in many cases, but typeof is simpler and usually more robust.
Another common issue is confusing an object with one of its callable properties. A value can have methods and still not be a function itself.
People also forget that classes report as function. That is normal in JavaScript, even though calling a class without new throws an error.
Finally, avoid using string-based function lookups unless absolutely necessary. If you are checking whether window[name] is a function, you are often one refactor away from fragile code.
Summary
- In JavaScript, the standard check is
typeof value === "function". - That works for normal, arrow, async, generator, and class function objects.
- Prefer
typeofoverinstanceof Functionin general code. - Use the check to validate callbacks before calling them.
- Do not confuse an object with a callable property on that object.
- "Function-like" and "safe to call" are related ideas, but they are not always identical.

