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

JavascriptWeb DevelopmentFront End Technology

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.

Example

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

<html>
   <head>
      <title>JavaScript Array join Method</title>
   </head>
   <body>
      <script>
         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 );
      </script>
   </body>
</html>
raja
Published on 23-Feb-2018 15:54:43
Advertisements