Skip to content
Related Articles

Related Articles

JavaScript Array join() function

Improve Article
Save Article
  • Last Updated : 21 Oct, 2022
Improve Article
Save Article

JavaScript Array.join() function is used to join the elements of the array together into a string. 

Syntax:

Array.join([separator])

Argument: (separator) A string to separate each element of the array. If leave it by default array element separated by comma( , ). 

Return Value: This function returns a string created by joining all the elements of the array using the separator. If the separator is not provided then the array elements are joined using a comma ( , ) as it is the default separator for this function. If an empty string is provided as the separator then the elements are joined directly without any character in between them. If the array is empty then this function returns an empty string. 

Example 1: 

JavaScript




// JavaScript code for join() function
<script>
    function func() {
    var a = [ 1, 2, 3, 4, 5, 6 ];
    console.log(a.join('|'));
    }
     
    func();
< /script>

Output:

1|2|3|4|5|6

Examples of the above function are provided below:

Example 1:

var a = [1, 2, 3, 4, 5, 6];
print(a.join('|'));

Output: In this example, the function join() joins together the elements of the array into a string using ‘|’.

1|2|3|4|5|6

Example 2:

var a = [1, 2, 3, 4, 5, 6];
print(a.join()); 

Output: In this example, the function join() joins together the elements of the array into a string using ‘, ‘ since it is the default value.

1, 2, 3, 4, 5, 6

Example 3:

var a = [1, 2, 3, 4, 5, 6];
print(a.join(''));

Output: In this example, the function join() joins together the elements of the array into a string using ‘ ‘ (empty string). 

123456

Codes for the above function are provided below:

Program 1: 

JavaScript




// JavaScript code for join() function
<script>
    function func() {
    var a = [ 1, 2, 3, 4, 5, 6 ];
    console.log    (a.join());
    }
     
    func();
< /script>

Output:

1, 2, 3, 4, 5, 6

Program 3: 

JavaScript




// JavaScript code for join() function
<script>
    function func() {
    var a = [ 1, 2, 3, 4, 5, 6 ];
    console.log(a.join(''));
    }
    func();
< /script>

Output:

123456

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!