Format lists
Display a list in a human-readable format.
JavaScript · Function
Array.prototype.join() returns a new string made by concatenating an array's elements, separated by commas or a chosen string. It returns an empty string for an empty array.
Call join() on an array to convert its elements to strings and concatenate them in order. The optional separator is placed between adjacent elements; omitted separators default to commas, while null and undefined elements contribute empty text.
join()
join(separator)| Name | Type | Description |
|---|---|---|
separator | string (optional) | A string inserted between each pair of adjacent array elements. If omitted, it defaults to a comma. |
A string containing all array elements joined together; an empty array returns the empty string.
const parts = ["Fire", "Air", "Water"];
console.log(parts.join(" - "));
// "Fire - Air - Water"Uses a custom separator to produce one readable string from the array.
Display a list in a human-readable format.
Create delimited text for logs or simple interchange formats.
Convert an array-like object into a delimited string.
join() returns a new string containing the array elements converted to strings and concatenated in order. With no argument, commas separate the elements; a supplied string becomes the separator.
Follow-up: What does join() return for an empty array? It returns the empty string.
A null or undefined element is converted to an empty string. This differs from ordinary string conversion, where those values would normally produce the text "null" or "undefined".
Follow-up: How are holes handled? Empty slots are treated as undefined, so they also contribute no text while still affecting separator placement.
join() recursively converts nested elements, including nested arrays, to strings. Only the outermost call uses the supplied separator; nested arrays use commas at deeper levels.
Follow-up: What happens when Array.prototype.toString() is called? It invokes join() without arguments, so arrays normally stringify with comma separators.
Array.prototype.toString()Array.prototype.toLocaleString()Array.prototype.concat()String.prototype.split()