What is the usage of join() method in JavaScript?

JavaScript array join() method joins all the elements of an array into a string.

The following is the parameter −

  • separator − Specifies a string to separate each element of the array. If omitted, the array elements are separated with a comma.


You can try to run the following code to learn how to work with join() method in JavaScript −

      <title>JavaScript Array join Method</title>
         var arr = new Array("Football","Hockey","Squash");

         var str = arr.join();
         document.write("str : " + str );

         var str = arr.join(", ");
         document.write("<br />str : " + str );

         var str = arr.join(" + ");
         document.write("<br />str : " + str );