Array join() Method - Javascript Array

Javascript examples for Array:join

Description

The join() method joins the array elements into a string, and returns the string.

The elements will be separated by a specified separator. The default separator is comma (,).

Syntax

array.join(separator);

Parameter Values

Parameter Description
separator Optional. The separator to be used. If omitted, the elements are separated with a comma

Return Value:

A String, representing the array values, separated by the specified separator

The following code shows how to Join the elements of an array into a string:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from  w w w  .  j a  va2s  .  c  o m
    var fruits = ["a","b","c","d","e"];
    var x = document.getElementById("demo");
    x.innerHTML = fruits.join(" and ");
}
</script>

</body>
</html>

Related Tutorials