Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.join()

BaselineWidely available

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

js
join()join(separator)

Parameters

separatorOptional

A 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.

js
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.

js
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.

js
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:

js
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.

js
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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp