Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • join() method of JavaScript Array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 125
    Comment on it

    JavaScript Array join() method : The join() method of the array is used to combine all elements of the array and return as a string by separating elements using the given parameter. By default the elements are separated by comma (,), we can pass the separator string as a parameter.


    Syntax of the Array join() method :

     array.join(separator)
    

    separator : Optional. This is the string which will separate the elements after joining them. If it is not passed then default is comma (,).


    Example of the Array join() method : Here I will show you how you can use the join() method to combine the all elements and get the all elements in a string object.

    Sample1.html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>To display all the elements as a string, click the button "show elements".</p>
    <button onclick="showIndex()">show elements</button>
    <p id="container"></p>
    
    <script>
    function showIndex() {
        var students = ["Ashok", "Nitin Kumar", "Rajesh", "Pankaj", "Sumit"];
        var result = students.join();
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    

    Output :

    Ashok,Nitin Kumar,Rajesh,Pankaj,Sumit
    

    In the above example the elements are separated by default separator comma. In the SampleExample2.html we will separate the elements using the and string.


    SampleExample2.html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>To display all the elements as a string seperated by string " and ", click the button "show elements".</p>
    <button onclick="showIndex()">show elements</button>
    <p id="container"></p>
    
    <script>
    function showIndex() {
        var students = ["Ashok", "Nitin Kumar", "Rajesh", "Pankaj", "Sumit"];
        var result = students.join(" and ");
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    

    Output :

    Ashok and Nitin Kumar and Rajesh and Pankaj and Sumit
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: