Javascript Reference - JavaScript Array join() Method








The join() method accepts the string separator as the only argument and returns a string containing all items.

The default separator is comma (,).

Browser Support

join() Yes Yes Yes Yes Yes

Syntax

array.join(separator)

Parameter Values

Optional separator used to separate the array element in the returned string.

If omitted, the elements are separated with a comma(,).

Return Value

It returns a String representing the array elements separated by the specified separator.





Example


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.

Example 2

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: