Lodash _.join() method
What you’ll learn
- How
_.join(array, [separator=','])turns a collection into one string with a delimiter between each element’s text form. - Why
nullandundefinedslots render as empty pieces (and how that differs from stringifying objects yourself). - When native
Array.prototype.joinis enough and when Lodash’s import still helps for consistency with other array utilities. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional prior read _.intersectionWith(); you should know that Array.prototype.join inserts the separator between every pair of adjacent elements, including when some slots stringify to empty text.
- You are comfortable with template literals or string concatenation for building messages in JavaScript.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.join is the string bridge from list data: turn numbers, labels, or mixed cells into a single line for logs, URLs, or quick-and-dirty CSV previews. It mirrors the native join contract while living next to other Lodash array helpers in your imports.
Delimiter control
Default comma, or pass any string (even multi-character) between rendered elements.
Empty slots
null and undefined become empty segments, not the words “null” or “undefined.”
Read-only
The array is not modified; you only receive a new primitive string.
Syntax
_.join(array, [separator=',']) - array: collection whose elements are stringified (array-like values are supported).
- separator: optional glue between elements; defaults to a comma when omitted.
- Returns: a single string; an empty array yields
"".
Default comma
Omit the separator (or pass undefined) to get the familiar comma-separated line.
import join from "lodash/join";
join(["a", "b", "c"]);
// "a,b,c" Custom separator
Use any string: pipe, newline, or a multi-character delimiter.
import join from "lodash/join";
join([1, 2, 3], " | ");
// "1 | 2 | 3" null, undefined, and objects
Missing values collapse to empty segments; plain objects stringify to [object Object] unless you format them first.
import join from "lodash/join";
join(["x", null, undefined, 1, { id: 1 }], "-");
// "x---1-[object Object]" 📋 _.join vs Array.prototype.join
| API | Note |
|---|---|
_.join(array, sep) | Lodash import; works with array-like values after internal coercion. |
array.join(sep) | Built-in on real arrays; same separator and empty-slot rules for dense arrays. |
Pitfalls to avoid
Objects do not JSON.stringify
Expect [object Object] unless you map rows with JSON.stringify, custom serializers, or dedicated CSV tooling.
Separator is not escaped
If a cell already contains your delimiter, readers cannot split the string reliably; escape or quote fields first.
Numbers become text
Joined output is always a string; convert back with split and Number if you need numeric arrays again.
❓ FAQ
Summary
- Purpose:
_.join(array, separator)builds one string from array elements with the given delimiter (comma by default). - Slots:
nullandundefinedbecome empty string pieces between separators. - Next: Lodash _.last(), _.intersectionWith() (earlier in this track), or the array methods hub.
For CSV-style output with quoting rules, build rows explicitly or use a dedicated CSV library; _.join only inserts the separator between String(element) values (with empty slots for null / undefined).
6 people found this page helpful
