Lodash _.toString() method
What you’ll learn
- Why
_.toString(null)and_.toString(undefined)return''—not'null'or'undefined'. - How
_.toString(-0)uniquely preserves the negative-zero sign. - How arrays are recursively joined and how Symbols sidestep the native
TypeError. - Where
_.toStringfits vsString(),JSON.stringify, and template literals.
Prerequisites
Comfort with the basic JavaScript coercion rules—String(x), x + '', and JSON.stringify—will make the differences click immediately.
- You know that
String(null)returns'null'in plain JS. - Try-it labs load lodash from a CDN; no install needed.
Overview
Algorithm: if value == null → ''; else if string → return as-is; else if array → recurse + join with commas; else if Symbol → Symbol.prototype.toString; else value + '' with -0 preserved.
Nullish → ''
Top-level null/undefined short-circuit to an empty string.
Sign-preserving
-0 stringifies to '-0'. Useful for logging IEEE-754 oddities.
Safe for Symbols
_.toString(Symbol('x')) returns 'Symbol(x)' instead of throwing.
Syntax
_.toString(value) - value: any value to convert.
- Returns: a string. Always. Never throws (even for Symbols/BigInts).
Lodash docs baseline
The three official examples—the one most reference articles get wrong is _.toString(null).
import toString from "lodash/toString";
console.log(
"null: " + JSON.stringify(toString(null)) + "\n" + // ""
"-0: " + JSON.stringify(toString(-0)) + "\n" + // "-0"
"[1, 2, 3]: " + JSON.stringify(toString([1, 2, 3])) // "1,2,3"
); Nullish, Symbols & BigInt—no throws
Where native coercion either lies (String(null) → 'null') or crashes ('' + Symbol(...)), _.toString stays predictable.
import toString from "lodash/toString";
console.log(
"null: " + JSON.stringify(toString(null)) + "\n" + // ""
"undefined: " + JSON.stringify(toString(undefined)) + "\n" + // ""
"Symbol(x): " + JSON.stringify(toString(Symbol("x"))) + "\n" + // "Symbol(x)"
"1n (BigInt): " + JSON.stringify(toString(1n)) + "\n" + // "1"
"NaN: " + JSON.stringify(toString(NaN)) + "\n" + // "NaN"
"Infinity: " + JSON.stringify(toString(Infinity)) // "Infinity"
);
try {
console.log("native: " + ("" + Symbol("x")));
} catch (err) {
console.log("native: throws -> " + err.message);
} Arrays: recursive flattening, joined with commas
Each element is run through baseToString, then concatenated with the implicit comma from Array.prototype.toString. Nested arrays flatten and—here’s the quirk—nullish elements inside an array become 'null'/'undefined', because the recursion bypasses the outer short-circuit.
import toString from "lodash/toString";
console.log(
"[1, [2, 3], 4]: " + JSON.stringify(toString([1, [2, 3], 4])) + "\n" +
"[null, 1, undefined]: " + JSON.stringify(toString([null, 1, undefined])) + "\n" +
"[42, ' is the answer']:" + JSON.stringify(toString([42, " is the answer."]))
); 📋 _.toString vs native conversions
| Input | _.toString | String() | JSON.stringify |
|---|---|---|---|
null | '' | 'null' | 'null' |
undefined | '' | 'undefined' | undefined |
-0 | '-0' | '0' | '0' |
NaN | 'NaN' | 'NaN' | 'null' |
[1, 2, 3] | '1,2,3' | '1,2,3' | '[1,2,3]' |
{ a: 1 } | '[object Object]' | '[object Object]' | '{"a":1}' |
Symbol('x') | 'Symbol(x)' | 'Symbol(x)' | undefined |
Pitfalls to avoid
null and undefined become ''
Many tutorials (and the lodash docs’ old examples) suggest you get 'null' and 'undefined'. You don’t. If you need the literal word back, reach for native String(x) instead.
Plain objects become '[object Object]'
No deep serialization happens. Use JSON.stringify if you need the structure or want to log nested data.
Comma join, not space join
_.toString([42, ' is the answer.']) returns '42, is the answer.'—with a comma. To stitch values manually, use arr.join('') or template literals.
Nullish inside arrays
Inside an array, null and undefined stringify as the literal words. Only top-level nullish input is short-circuited to ''.
❓ FAQ
Summary
- Purpose: safe, total string conversion—never throws, always returns a string.
- Remember: top-level
null/undefined→'',-0→'-0', arrays recurse and join with commas, Symbols don’t throw. - Next: you’ve completed the Lang track. Move on to Lodash Math Methods for numeric helpers like
_.add,_.round, and_.maxBy.
_.toString(-0) returns the literal string '-0'—the only built-in conversion in JavaScript that preserves the sign bit of negative zero. Native String(-0), (-0).toString(), and -0 + '' all return plain '0'.
6 people found this page helpful
