JavaScript > Arrays

Array Join in JavaScript

How to join elements of an array into a string and returns it in JavaScript?


Array Join

The JavaScript Array Join {join()method} joins the elements of an array into a string. Joining the elements of an array into a string can be possible by using join() method, the parameter value is optional in the join() method, that means if we don't give any input value (Separator) to the join() method, it returns the joining elements of an array into a string, which are seperated by comma (, by default), or if we give some separator to the join() method, it joins all the elements which are seperated by a specific seperator and return as a string.

array.join(separator) are the parameters.

In JavaScript, by using join() method we can join the elements of an array into a string and returns the string.

Joining the elements of an array into a string.(Without giving seperator to join() method)

<p>Click the below button to to join the elements of an array into a string.</p>
<input type="button" onclick="myValue()" value="Join"/>
<p id="myId"></p>

<script>
    function myValue() {
        var a = ["France", "Egypt", "Darziling", "China", "Bangladesh", "Abudhabi"];
        var b = a.join();
        document.getElementById("myId").innerHTML = b;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function myValue() in the<script>block which is connected to the onclick of the HTML button and array with value "["France", "Egypt", "Darziling", "China", "Bangladesh", "Abudhabi"]" to the variable a. We need to join the elements of an array into a string and returns the string, for that we are using join() method, in this case we didnot used any separator, so the join() method seperates the elements of array with default separator (,) while returning. Onclick of the button ''Join" in the HTML code fires the function myValue() in the <script> block at the same time join() method joins the elements of an array into a string by separating the each element with a comma operator, and returns the string as a output.

OUTPUT

Joining the elements of an array into a string.(With seperator to join() method) 

<p>Click the below button to to join the elements of an array into a string.</p>
<input type="button" onclick="myValue()" value="Join"/>
<p id="myId"></p>

<script>
    function myValue() {
        var a = ["France", "Egypt", "Darziling", "China", "Bangladesh", "Abudhabi"];
        var b = a.join(" OR ");
        document.getElementById("myId").innerHTML = b;
    }
</script>

Joining elelemts with separator

In the above code snippet we used "OR" separator in join() method. Onclick of the button ''Join" in the HTML code fires the function myValue() in the <script> block at the same time join() method joins the elements of an array into a string by separting the each element with specified separator, and returns the new string as a output. 

OUTPUT

 Views: 4761 | Post Order: 162



Write for us






Hosting Recommendations