JavaScript | Array join() function

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 elements of the array. If leave it by default array element separate 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 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 array is empty then this function returns an empty string.

Examples for the above function are provided below:

Example 1:

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

Output:

1|2|3|4|5|6

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

Example 2:

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

Output:

1, 2, 3, 4, 5, 6

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

Example 3:

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

Output:


123456

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

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1|2|3|4|5|6

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1, 2, 3, 4, 5, 6

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

123456


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.