How Lodash isBoolean Checks Primitives and Objects
Lodash’s _.isBoolean method provides a reliable way to
verify whether a given value is either a native boolean primitive or a
Boolean wrapper object. Standard JavaScript type checking
often falls short because primitive booleans and Boolean
objects yield different results under basic operators. This article
examines the internal implementation of _.isBoolean,
detailing how it combines strict equality checks, object classification,
and internal tag inspection to ensure accurate type detection without
common pitfalls.
The Limitation of Standard JavaScript Checks
In vanilla JavaScript, checking for a boolean is not always
straightforward due to the distinction between boolean primitives and
Boolean objects created via the constructor:
typeof true; // 'boolean'
typeof new Boolean(true); // 'object'
new Boolean(false) instanceof Boolean; // trueWhile typeof identifies primitives, it classifies
new Boolean(...) as an object. Conversely,
instanceof Boolean fails when dealing with values
originating from different execution contexts, such as iframes or
different window realms, because the prototypes do not share the same
memory reference.
How Lodash Implements
_.isBoolean
Lodash solves this problem by using a multi-step evaluation that safely covers both primitives and complex object wrappers. Internally, the implementation follows this pattern:
function isBoolean(value) {
return value === true || value === false ||
(isObjectLike(value) && baseGetTag(value) === '[object Boolean]');
}1. The Fast Path: Strict Primitive Comparison
The first step checks if the input matches literal boolean values directly:
value === true || value === falseThis acts as a fast-path optimization. Because the vast majority of
boolean checks in real-world applications involve primitive
true or false, this comparison handles most
operations in constant time without delegating to helper functions or
inspecting object prototypes.
2. Guarding with
isObjectLike
If the value is not a primitive boolean, Lodash checks if it could be
a wrapped object using isObjectLike:
function isObjectLike(value) {
return typeof value === 'object' && value !== null;
}This step ensures that non-object primitives (such as numbers,
strings, and symbols) as well as null are discarded
immediately, preventing unnecessary lookups on invalid types.
3. Cross-Realm
Inspection via baseGetTag
For values that pass isObjectLike, Lodash determines
their true internal type using baseGetTag. This utility
relies on the internal Object.prototype.toString
method:
Object.prototype.toString.call(value); // Returns '[object Boolean]' for Boolean objectsUsing Object.prototype.toString inspects the internal
[[Class]] property of the object. Unlike
instanceof, this approach is cross-realm safe, meaning a
Boolean object created in an iframe or worker will still
return '[object Boolean]' in the host context.
Lodash also handles modern JavaScript features like
Symbol.toStringTag. If an object attempts to disguise
itself using [Symbol.toStringTag], Lodash accounts for this
by temporarily masking the custom tag to inspect the underlying native
type safely.
Key Benefits of This Approach
- Comprehensive Detection: Accurately recognizes
true,false,new Boolean(true), andnew Boolean(false). - Zero Falsy Confusion: Values such as
0,"",null, andundefinedare strictly rejected without being coerced. - Environment Agnostic: Works consistently across different execution contexts, such as multiple iframes, Node.js VMs, or web workers.