join()
method creates and returns a new string by concatenating all of the elements in an array (or an
array-like object
), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
arr.join([separator])
separator
可选
separator
is an empty string, all elements are joined without any characters in between them.
A string with all array elements joined. If
arr
.length
is
0
, the empty string is returned.
The string conversions of all array elements are joined into one string.
If an element is
undefined
,
null
or an empty array
[]
, it is converted to an empty string.
The following example creates an array,
a
, with three elements, then joins the array four times: using the default separator, then a comma and a space, then a plus and an empty string.
var a = ['Wind', 'Water', 'Fire'];
a.join(); // 'Wind,Water,Fire'
a.join(', '); // 'Wind, Water, Fire'
a.join(' + '); // 'Wind + Water + Fire'
a.join(''); // 'WindWaterFire'
The following example joins array-like object (
arguments
), by calling
Function.prototype.call
on
Array.prototype.join
.
function f(a, b, c) {
var s = Array.prototype.join.call(arguments);
console.log(s); // '
}
f(1, 'a', true);
//expected output: "1,a,true"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array.prototype.join' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
join
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Array
Array.from()
Array.isArray()
Array.of()
Array.prototype.concat()
Array.prototype.copyWithin()
Array.prototype.entries()
Array.prototype.every()
Array.prototype.fill()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.forEach()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.keys()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toSource()
Array.prototype.toString()
Array.prototype.unshift()
Array.prototype.values()
Array.prototype[@@iterator]()
get Array[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()