The join()
method accepts the string separator as
the only argument and returns a string containing all items.
The default separator is comma (,).
join() |
Yes | Yes | Yes | Yes | Yes |
array.join(separator)
Optional separator
used to
separate the array element in the returned string.
If omitted, the elements are separated with a comma(,).
It returns a String representing the array elements separated by the specified separator.
var colors = ["red", "green", "blue"];
console.log(colors.join(",")); //red,green,blue
console.log(colors.join("||")); //red||green||blue
The code above generates the following result.
The following code shows how to use the default separator to join the elements in an array into a string.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">join</button>
<!--from w ww. j av a 2s. c o m-->
<p id="demo"></p>
<script>
function myFunction() {
var array1 = ["B", "A", "C", "M"];
var x = document.getElementById("demo");
x.innerHTML = array1.join();
}
</script>
</body>
</html>
The code above is rendered as follows: