How Lodash isBoolean Evaluates new Boolean()

This article explains how Lodash’s _.isBoolean method evaluates objects created with the new Boolean() constructor. It covers the difference between primitive booleans and boolean object wrappers, details the internal mechanisms Lodash uses to identify these objects, and highlights practical implications for JavaScript developers.

In standard JavaScript, using the new Boolean() constructor produces an object wrapper rather than a boolean primitive. When evaluated with native JavaScript operators such as typeof, an instance created via new Boolean(true) or new Boolean(false) returns 'object'. Consequently, a standard check like typeof value === 'boolean' evaluates to false for constructor-generated instances.

Lodash’s _.isBoolean function is explicitly designed to identify both primitive boolean values and boolean wrapper objects. When evaluated with _.isBoolean(new Boolean()), the method returns true.

Internally, Lodash achieves this by verifying two criteria:

  1. Direct equality with primitive values (value === true || value === false).
  2. An internal tag check for object-based booleans.

If the value is not a primitive boolean, Lodash checks if the value is "object-like" (non-null and with a typeof equal to 'object') and inspects its internal [[Class]] tag. Lodash uses an internal helper, often equivalent to calling Object.prototype.toString.call(value).

For any instance initialized via new Boolean(), Object.prototype.toString.call(instance) yields the string "[object Boolean]". Because the internal tag matches, Lodash classifies the instance as a boolean and returns true.

While _.isBoolean correctly identifies these wrappers as boolean types, developers must still exercise caution. In standard conditional logic (such as an if statement), any object—including new Boolean(false)—evaluates as truthy. To extract the actual primitive boolean value from an instance evaluated by _.isBoolean, you must call its .valueOf() method.