The string conversions of all array elements are joined into one string. If an element is undefined
, null
, it is converted to an empty string instead of the string "null"
or "undefined"
.
The join
method is accessed internally by Array.prototype.toString()
with no arguments. Overriding join
of an array instance will override its toString
behavior as well.
Array.prototype.join
recursively converts each element, including other arrays, to strings. Because the string returned by Array.prototype.toString
(which is the same as calling join()
) 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());
console.log(matrix.join(";"));
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(";"));
When used on sparse arrays, the join()
method iterates empty slots as if they have the value undefined
.
The join()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties.