JavaScript Reference ยท Array
JavaScript Array join() method
Reference for JavaScript Array join(): combine array elements into a string with a separator, how null, undefined and nested arrays are converted, plus examples.
The join() method turns an array into a single string by converting every element to a string and placing a separator between them. With no argument the separator is a comma. The array itself is not changed.
It is the inverse of String.prototype.split(), and it is the usual way to build comma-separated lists, paths and multi-line text.
["a", "b", "c"].join("-"); // "a-b-c"
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
join() join(separator)
| Name | Description |
|---|---|
separator |
Optional. A string placed between elements. Default ",". Other values are converted to strings; undefined means the default. An empty string joins with nothing in between. |
Return value: a string. An empty array gives "", and a one-element array gives that element as a string, with no separator.
Exceptions: a TypeError when converting an element fails, for example a Symbol. An array that contains itself does not loop forever: the cyclic reference becomes an empty string.
Usage notes
nullandundefinedbecome empty strings, and so do holes:[1, null, 3].join("-")is"1--3". Usemap()first if you need a placeholder.- Nested arrays are joined with commas, because each inner array is converted with its own
toString(), which callsjoin()with the default separator. The;you passed applies only to the outer level. - Objects become
"[object Object]"unless they definetoString(). Map them to the text you want before joining. - Numbers use their normal string form:
[0.1 + 0.2].join()gives"0.30000000000000004". Format first when you need fixed decimals. toString()isjoin()with the default comma, and string concatenation of an array ("" + arr) does the same.- Building big strings:
join()on an array of parts is efficient and tidy, but template literals are better for a few known pieces. - Escaping:
join()does not escape separators. For real CSV, quote fields that contain the separator.
Browser support
join() is supported in every current browser and has been for a very long time.