Lodash _.isFunction() method
What you’ll learn
- How
_.isFunction(value)recognizes callable objects beyond naive checks. - Which variants count (sync, arrow-like, async, generators, proxies).
- Why ordinary POJOs, primitives, and regex literals fail.
- When lodash tagging aligns or differs from
typeof.
Prerequisites
You can declare JavaScript functions and know basic callable terminology.
- You understand arrow versus traditional function declarations.
- Try-it labs load lodash from the CDN.
Overview
Use _.isFunction before invoking callbacks, normalizing middleware, or guarding Higher-Order API surfaces—especially when unknown payloads pretend to be callables.
Tagged callables
Matches Function, GeneratorFunction, AsyncFunction, Proxy callables.
Safe guards
Filter hooks before fn() reduces runtime TypeErrors.
Non-callables out
Plain objects, regex literals, primitives exit fast.
Syntax
_.isFunction(value) - value: any value to test.
- Returns:
trueif classified as a Function-like callable per lodash tags; otherwisefalse.
Traditional and arrow functions
Both declaration styles register as native callables.
import isFunction from "lodash/isFunction";
console.log(
"namedFn: " + isFunction(function bump(n) { return n + 1; }) + "\n" + // true
"arrowFn: " + isFunction(() => 42) // true
); Objects, regex literals, and primitives
These values lack Function tagging even when they relate to behavior elsewhere.
import isFunction from "lodash/isFunction";
console.log(
"plainObj: " + isFunction({ run: function () {} }) + "\n" + // false (object, not the inner fn)
"regexp: " + isFunction(/match-me/) + "\n" + // false
"numberPrim: " + isFunction(0) // false
); Async functions and class constructors
Modern callable forms remain detected; ES classes are constructors.
import isFunction from "lodash/isFunction";
console.log(
"asyncFn: " + isFunction(async function fetchData() {}) + "\n" + // true
"classCtor: " + isFunction(class Widget {}) // true
); 📋 _.isFunction vs related checks
| API | Matches |
|---|---|
_.isFunction(x) | Tagged Function / Generator / Async / Proxy callables. |
typeof x === "function" | Often matches; lodash centralizes edge-case tagging rationale. |
x instanceof Function | Generally aligned for ordinary functions; odd prototypes may diverge. |
_.isObject(x) | Broader—includes non-callables that still pass object checks. |
Pitfalls to avoid
Properties holding functions
_.isFunction(obj) inspects obj itself—grab obj.handler before testing.
Host objects
Some browsers expose callable DOM APIs—behavior follows engine tagging; validate expectations manually.
Instances versus constructors
The class body is a function; instances are ordinary objects unless explicitly callable.
❓ FAQ
Summary
- Purpose: classify values as callable Function objects across modern variants.
- Remember: test the exact reference you intend to invoke—not its parent object.
- Next: explore more on Lodash _.isInteger().
_.isFunction classifies values whose tags are [object Function], [object GeneratorFunction], [object AsyncFunction], or [object Proxy]—so ES classes (class Foo {}) register as functions because they are callable constructors.
6 people found this page helpful
