Lodash _.toString() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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 _.toString fits vs String(), 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

javascript
_.toString(value)
  • value: any value to convert.
  • Returns: a string. Always. Never throws (even for Symbols/BigInts).
1

Lodash docs baseline

The three official examples—the one most reference articles get wrong is _.toString(null).

javascript
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"
);
Try it Yourself
2

Nullish, Symbols & BigInt—no throws

Where native coercion either lies (String(null)'null') or crashes ('' + Symbol(...)), _.toString stays predictable.

javascript
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);
}
Try it Yourself
3

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.

javascript
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."]))
);
Try it Yourself

📋 _.toString vs native conversions

Input_.toStringString()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

Nullish

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.

Objects

Plain objects become '[object Object]'

No deep serialization happens. Use JSON.stringify if you need the structure or want to log nested data.

Arrays

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.

Quirk

Nullish inside arrays

Inside an array, null and undefined stringify as the literal words. Only top-level nullish input is short-circuited to ''.

❓ FAQ

An empty string ''. Same for undefined. This is the single most-missed fact about the method—native String(null) returns 'null', but _.toString(null) returns ''.
No. _.toString(Symbol('x')) returns 'Symbol(x)' and _.toString(1n) returns '1'. The native '' + Symbol(...) coercion throws TypeError; lodash routes Symbols through Symbol.prototype.toString instead.
Lodash's baseToString explicitly detects -0 via 1 / value === -Infinity and returns the literal string '-0'. Native String(-0) and -0 + '' both return '0'.
Recursively. Each element is run through baseToString and the result is joined with commas, just like Array.prototype.toString. Nested arrays flatten: [1,[2,3]] becomes '1,2,3'. Nullish elements inside an array become 'null' / 'undefined' (the recursion bypasses the outer null short-circuit).

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.
Did you know?

_.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'.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful