Lodash _.isSymbol() method
What you’ll learn
- How
_.isSymbol(value)detects Symbol primitives and boxed Symbol objects. - Why well-known symbols like
Symbol.iteratorqualify. - How this is broader than
typeof value === "symbol". - Where Symbol guards belong—protocol keys, registry lookups, and meta-programming.
Prerequisites
You know Symbol() creates a unique primitive used for protocol keys and private fields.
- You have seen
Symbol.iteratororSymbol.forin practice. - Try-it labs load lodash from the CDN.
Overview
Reach for _.isSymbol when an API accepts symbolic keys, protocol identifiers, or sentinel values—and you must reject regular strings or numbers used by mistake.
Primitive + boxed
Both Symbol() and Object(Symbol()) qualify.
Well-known symbols
Symbol.iterator, Symbol.asyncIterator, etc. all pass.
Strings excluded
Description strings like "example" never qualify.
Syntax
_.isSymbol(value) - value: any value to test.
- Returns:
truewhen value is a Symbol primitive or boxed Symbol object; otherwisefalse.
Symbol primitives
The lodash docs example: well-known symbols like Symbol.iterator qualify, alongside fresh Symbol() values.
import isSymbol from "lodash/isSymbol";
console.log(
"iter: " + isSymbol(Symbol.iterator) + "\n" + // true (lodash docs)
"fresh: " + isSymbol(Symbol("example")) + "\n" + // true
"forKey: " + isSymbol(Symbol.for("shared")) // true
); Boxed Symbol objects
Wrapping a Symbol via Object() makes its typeof become "object", but _.isSymbol still returns true via the tag check.
import isSymbol from "lodash/isSymbol";
const wrapped = Object(Symbol("wrapped"));
console.log(
"boxed: " + isSymbol(wrapped) + "\n" + // true
"typeofRaw: " + (typeof wrapped) // "object"
); Strings, numbers, and nullish
Lodash docs show _.isSymbol("abc") as false—a string description never makes a value symbolic.
import isSymbol from "lodash/isSymbol";
console.log(
"abc: " + isSymbol("abc") + "\n" + // false (lodash docs)
"num: " + isSymbol(42) + "\n" + // false
"obj: " + isSymbol({}) + "\n" + // false
"nullVal: " + isSymbol(null) // false
); 📋 _.isSymbol vs related checks
| API / pattern | Behavior |
|---|---|
_.isSymbol(x) | true for primitive symbols and boxed Symbol objects. |
typeof x === "symbol" | Primitive only—misses Object(Symbol()) wrappers. |
Object.prototype.toString.call(x) | Tag [object Symbol]—the same idea lodash wraps. |
Symbol.keyFor(x) | Identifies Symbol.for-registered symbols only. |
Pitfalls to avoid
for...in skips symbols
Use Object.getOwnPropertySymbols(obj) to iterate symbol keys; a plain for…in won’t see them.
Unique by construction
Symbol("x") === Symbol("x") is false—use Symbol.for("x") for shared registry keys.
Not serializable
Symbols are dropped by JSON.stringify; convert to strings or skip in your serializer.
❓ FAQ
Summary
- Purpose: recognize Symbol primitives and boxed wrappers before symbol-aware operations.
- Remember: strings, numbers, and nullish values never qualify.
- Next: explore more on Lodash _.isTypedArray().
Well-known symbols like Symbol.iterator and Symbol.asyncIterator are real Symbol primitives—_.isSymbol(Symbol.iterator) returns true per the lodash docs.
6 people found this page helpful
