Skip to content
Related Articles

Related Articles

JavaScript Array join() Method

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 01 Nov, 2021
View Discussion
Improve Article
Save Article

Below is the example of the Array join() method.

  • Example:




    <script> 
       function func() { 
          var a = [ 1, 2, 3, 4, 5, 6 ]; 
          document.write(a.join('|')); 
       
       func(); 
    < /script> 

  • Output:
    1|2|3|4|5|6

The arr.join() method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(, ).

Syntax:

array.join(separator)

Parameters: This method accept single parameter as mentioned above and described below:

  • separator: It is Optional i.e, it can be either used as parameter or not.Its default value is comma(, ).
  • Return Value: It returns the String which contain the collection of array’s elements.

    Below example illustrate the Array join() method in JavaScript:

    • Example 1: In this example the function join() joins together the elements of the array into a string using ‘|’.
      var a = [1, 2, 3, 4, 5, 6];
      print(a.join('|'));

      Output:

      1|2|3|4|5|6
    • Example 2: In this example the function join() joins together the elements of the array into a string using ‘, ‘ since it is the default value.
      var a = [1, 2, 3, 4, 5, 6];
      print(a.join()); 

      Output:

      1, 2, 3, 4, 5, 6
    • Example 3: In this example the function join() joins together the elements of the array into a string using ‘ ‘ (empty string).
      var a = [1, 2, 3, 4, 5, 6];
      print(a.join(''));

      Output:

      123456

    Code for the above method is provided below:
    Program 1:




    <script> 
       function func() { 
          var a = [ 1, 2, 3, 4, 5, 6 ]; 
          document.write(a.join()); 
       
      
       func(); 
    </script> 

    Output:

    1, 2, 3, 4, 5, 6

    Program 2:




    <script> 
       function func() { 
          var a = [ 1, 2, 3, 4, 5, 6 ]; 
          document.write(a.join('')); 
       
       func(); 
    </script> 

    Output:

    123456

    Supported Browsers: The browsers supported by JavaScript Array join() method are listed below:

    • Google Chrome 1.0 and above
    • Microsoft Edge 12 and above
    • Mozilla Firefox 1.0
    • Safari 1 and above
    • Opera 4 and above

    My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!