Array.prototype.join
Joins array elements into a string.
Available since: ES1
Syntax
array.join(separator)Parameters
| Parameter | Type | Description |
|---|---|---|
| separator | string | Optional. The string placed between the elements; a comma by default. |
Return value
stringA string of the array elements.
Examples
A list separated by a comma and a space:
console.log(['red', 'green', 'blue'].join(', '));Output:
red, green, blueJoining without a separator:
console.log(['J', 'S'].join(''));Output:
JSNotes
null and undefined become an empty string. split() does the reverse.
See also
- String.prototype.split — Splits a string into an array by a separator.
- Array.prototype.map — Creates a new array by transforming every element.
- Array.prototype.concat — Merges arrays into a new array.
Related recipes
In the tutorial
- Array methods — How to add and remove array elements in JavaScript: push, pop, shift, unshift, slice, splice, concat and join.