Lodash _.isBoolean() method
What you’ll learn
- How
_.isBoolean(value)classifies boolean primitives and wrappers. - Why truthy/falsy checks are not boolean type checks.
- How this compares to
typeof value === "boolean". - How to validate booleans from JSON or query strings safely.
Prerequisites
Comfort with boolean literals, truthiness, and the difference between types and runtime coercion.
- You understand
typeofversus object wrappers. - You can open Try-it labs in the browser.
Overview
_.isBoolean is the lodash helper for strict boolean typing—ideal for validating flags in configuration objects, parsed JSON, or user-controlled inputs without treating numbers or strings as booleans by accident.
Primitives
true and false literals always pass.
Boxed Booleans
new Boolean(...) is detected—unlike a bare typeof check.
Not truthiness
0, "", and null stay non-booleans even when falsy.
Syntax
_.isBoolean(value) - value: any value to test.
- Returns:
trueifvalueis a boolean primitive orBooleanobject; otherwisefalse.
Boolean primitives
Both boolean literals classify cleanly—this is the common case for flags and predicates.
import isBoolean from "lodash/isBoolean";
isBoolean(true); // true
isBoolean(false); // true Boxed Booleans versus impostors
new Boolean(false) is still typed as a Boolean wrapper. Numbers and strings—even when they look like flags—fail.
import isBoolean from "lodash/isBoolean";
isBoolean(new Boolean(false)); // true (boxed)
isBoolean(1); // false
isBoolean("true"); // false Boolean(...) versus boolean typing
The Boolean function coerces values into primitives—those results are booleans. Plain numbers remain non-booleans even when truthy.
import isBoolean from "lodash/isBoolean";
isBoolean(Boolean(0)); // true — primitive false
isBoolean(42); // false — still a number
isBoolean(JSON.parse("true")); // true — JSON boolean literal 📋 _.isBoolean vs other checks
| Approach | Booleans detected |
|---|---|
_.isBoolean(x) | Primitives + Boolean objects. |
typeof x === "boolean" | Primitives only. |
x === true || x === false | Strictly the two literals. |
| Truthy / falsy tests | Many non-boolean types; not a substitute. |
Pitfalls to avoid
Checkbox DOM values
Unchecked boxes often omit keys or submit strings—normalize before treating inputs as booleans.
new Boolean(false) is truthy
Boxed false still behaves like an object in conditionals; unwrap with Boolean(x) or avoid wrappers entirely.
Validation libraries
For complex payloads prefer schema validators; use _.isBoolean for quick lodash-native guards.
❓ FAQ
Summary
- Purpose: strict boolean typing including wrappers.
- Not: a replacement for truthiness or numeric zero checks.
- Next: explore more on Lodash _.isBuffer().
typeof false === "boolean" only catches primitives. Lodash’s _.isBoolean also returns true for values created with new Boolean(...), which typeof reports as "object".
6 people found this page helpful
