Join array elements together with string separator in JavaScript

Description

The following code shows how to join array elements together with string separator.

Example


<!--from  www. j  av  a 2 s . c o  m-->

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var colors = ["red", "green", "blue"];
document.writeln(colors.join(",")); //red,green,blue
document.writeln("<br/>");
document.writeln(colors.join("||")); //red||green||blue

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Join array elements together with string separator in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      Array
...
Create an array with different type of valu...
Create an array without setting its length ...
Create an empty array with array literal in...
Create array with Array Constructor and set...
Create array with Array Constructor and set...
Create array with array literal notation in...
Create your own function for sorting in Jav...
Delete an array element in JavaScript
Delete with Array splice() in JavaScript
Display Multi-dimensional array in a HTML t...
Do an alphabetical sort() method on strings...
Fill and populate two dimensional array in ...
Filter an array with a condition in JavaScr...
Get a string of the array elements separate...
Get array element value with index in JavaS...
Get the last element in an Array in JavaScr...
Insert with Array splice() in JavaScript
Integer Array Declaration with initializati...
Join array elements together with string se...
Loop through an array with for statement in...
Loop through array and work on each element...
Map array value in JavaScript
Move data from an Array to another in JavaS...
Output array with toString() in JavaScript
Output array with valueOf() in JavaScript
Push form value to a stack in JavaScript
Push two values to an empty array in JavaSc...
Reduce an array from the end with reduceRig...
Reduce array to a value in JavaScript
Reference array element by random index val...
Remove elements from array by changing the ...
Remove the first item in the array and retu...
Replace array element with Array splice() i...
Reverse an array in JavaScript
Search an array for an object with indexOf ...
Search an array from back at a specific end...
Search an array from start with indexOf met...
Search an array from starting index with in...
...