The method array.toString()
actually calls array.join()
which result in a string concatenated by commas. ref
var array = ['a','b','c','d','e','f'];_x000D_
document.write(array.toString()); // "a,b,c,d,e,f"
_x000D_
Also, you can implicitly call Array.toString()
by making javascript coerce the Array
to an string
, like:
//will implicitly call array.toString()
str = ""+array;
str = `${array}`;
The join() method joins all elements of an array into a string.
It accepts a separator
as argument, but the default is already a comma ,
str = arr.join([separator = ','])
var array = ['A', 'B', 'C'];
var myVar1 = array.join(); // 'A,B,C'
var myVar2 = array.join(', '); // 'A, B, C'
var myVar3 = array.join(' + '); // 'A + B + C'
var myVar4 = array.join(''); // 'ABC'
If any element of the array is undefined or null , it is treated as an empty string.
It is available pretty much everywhere today, since IE 5.5 (1999~2000).