Sunday 2 September 2018

JavaScript: join: Join element of the array

join method is used to join all the elements of the array using given separator.

Syntax
arrayName.join(separator)

Ex
var array = [2, 3, 5, 7, 11, 13];
                          
array.join(",") : Join all the elements in array using ,
array.join(":") : Join all the elements in array using :
array.join("abb") : Join all the elements in array using abb

array.html
<!DOCTYPE html>

<html>

<head>
    <title>Array</title>
</head>

<body>
    <script type="text/javascript">
        var array = [2, 3, 5, 7, 11, 13];

        var data1 = array.join(",");
        var data2 = array.join(":");
        var data3 = array.join("abb");

        document.write("data1 = " + data1 + "<br />");
        document.write("data2 = " + data2 + "<br />");
        document.write("data3 = " + data3 + "<br />");
    </script>
</body>

</html>

Open above page in browser, you can able to see following content.

data1 = 2,3,5,7,11,13
data2 = 2:3:5:7:11:13
data3 = 2abb3abb5abb7abb11abb13



Previous                                                 Next                                                 Home

No comments:

Post a Comment