Programming
Variable Detection
Function Identification
Coding Tips
JavaScript

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.

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

That works for:

  • normal functions
  • arrow functions
  • async functions
  • generator functions
  • class constructors, because JavaScript classes are functions internally

Example:

javascript
1const arrow = () => 1;
2async function fetchData() {}
3function* ids() {
4  yield 1;
5}
6class Person {}
7
8console.log(typeof arrow);
9console.log(typeof fetchData);
10console.log(typeof ids);
11console.log(typeof Person);

All of those print function.

Why typeof Is Usually Better Than instanceof

You may also see this pattern:

javascript
value instanceof Function

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.

javascript
function isFunction(value) {
  return typeof value === "function";
}

That helper is simple, readable, and correct for most real-world needs.

Practical Callback Guarding

The most common use case is guarding optional callbacks.

javascript
1function runTask(taskName, onDone) {
2  console.log("running", taskName);
3
4  if (typeof onDone === "function") {
5    onDone(taskName);
6  }
7}
8
9runTask("build", (name) => {
10  console.log("finished", name);
11});

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.

javascript
1function registerHandler(handler) {
2  if (typeof handler !== "function") {
3    throw new TypeError("handler must be a function");
4  }
5
6  handler();
7}

When a Value Looks Callable but Is Not

Some values are easy to mistake for functions:

  • objects that contain a call method
  • objects returned by libraries that represent deferred work
  • strings containing function names
  • methods accessed incorrectly and therefore evaluated to undefined

For example:

javascript
1const obj = {
2  call() {
3    console.log("not actually a function value");
4  }
5};
6
7console.log(typeof obj === "function");
8console.log(typeof obj.call === "function");

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.

javascript
function check(value) {
  return Object.prototype.toString.call(value) === "[object Function]";
}

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 typeof over instanceof Function in 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.

Course illustration
Course illustration

All Rights Reserved.