Array.prototype.join()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thejoin()
method ofArray
instances creates andreturns a new string by concatenating all of the elements in this array,separated by commas or a specified separator string. If the array hasonly one item, then that item will be returned without using the separator.
Try it
const elements = ["Fire", "Air", "Water"];console.log(elements.join());// Expected output: "Fire,Air,Water"console.log(elements.join(""));// Expected output: "FireAirWater"console.log(elements.join("-"));// Expected output: "Fire-Air-Water"
Syntax
join()join(separator)
Parameters
separator
OptionalA string to separate each pair of adjacent elements of the array. If omitted, the array elements are separated with a comma (",").
Return value
A string with all array elements joined. Ifarray.length
is0
, the empty string is returned.
Description
The string conversions of all array elements are joined into one string. If an element isundefined
ornull
, it is converted to an empty string instead of the string"null"
or"undefined"
.
Thejoin
method is accessed internally byArray.prototype.toString()
with no arguments. Overridingjoin
of an array instance will override itstoString
behavior as well.
Array.prototype.join
recursively converts each element, including other arrays, to strings. Because the string returned byArray.prototype.toString
(which is the same as callingjoin()
) does not have delimiters, nested arrays look like they are flattened. You can only control the separator of the first level, while deeper levels always use the default comma.
const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9],];console.log(matrix.join()); // 1,2,3,4,5,6,7,8,9console.log(matrix.join(";")); // 1,2,3;4,5,6;7,8,9
When an array is cyclic (it contains an element that is itself), browsers avoid infinite recursion by ignoring the cyclic reference.
const arr = [];arr.push(1, [3, arr, 4], 2);console.log(arr.join(";")); // 1;3,,4;2
When used onsparse arrays, thejoin()
method iterates empty slots as if they have the valueundefined
.
Thejoin()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties.
Examples
Joining an array four different ways
The following example creates an array,a
, with three elements, then joinsthe array four times: using the default separator, then a comma and a space, then a plusand an empty string.
const a = ["Wind", "Water", "Fire"];a.join(); // 'Wind,Water,Fire'a.join(", "); // 'Wind, Water, Fire'a.join(" + "); // 'Wind + Water + Fire'a.join(""); // 'WindWaterFire'
Using join() on sparse arrays
join()
treats empty slots the same asundefined
and produces an extra separator:
console.log([1, , 3].join()); // '1,,3'console.log([1, undefined, 3].join()); // '1,,3'
Calling join() on non-array objects
Thejoin()
method reads thelength
property ofthis
and then accesses each property whose key is a nonnegative integer less thanlength
.
const arrayLike = { length: 3, 0: 2, 1: 3, 2: 4, 3: 5, // ignored by join() since length is 3};console.log(Array.prototype.join.call(arrayLike));// 2,3,4console.log(Array.prototype.join.call(arrayLike, "."));// 2.3.4
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.join |