What does .join do in javascript?

Examples

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.join();

Try it Yourself »

Another separator:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.join(" and ");

Try it Yourself »


Definition and Usage

The join() method returns an array as a string.

The join() method does not change the original array.

Any separator can be specified. The default is comma (,).


Syntax

Parameters

Parameter Description
separator Optional.
The separator to be used.
Default is a comma.

Return Value

Type Description
A string. The array values, separated by the specified separator.


Browser Support

join() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

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

    • Example:

    < /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:

         

         func(); 

       

      Output:

      1, 2, 3, 4, 5, 6

      Program 2:

       

      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

    How do you split and join in JavaScript?

    How it works: First, split the title string by the space into an array by using the split() string method. Second, concatenate all elements in the result array into a string by using the join() method. Third, convert the result string to lower case by using the toLowerCase() method.

    What does .split do in JavaScript?

    The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

    How do you combine elements into an array of strings?

    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(, ).

    How do you turn an array into a string?

    Using StringBuffer.
    Create an empty String Buffer object..
    Traverse through the elements of the String array using loop..
    In the loop, append each element of the array to the StringBuffer object using the append() method..
    Finally convert the StringBuffer object to string using the toString() method..