Open In App

JavaScript Array join() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The JavaScript Array.join() method is used to join the elements of the array together 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: 

  • [separator]: A string to separate each element of the array. By default, the array elements are separated by a 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: In this example, we will be joining the elements of an array using ( | ) by join() method.

JavaScript




function func() {
    let a = [1, 2, 3, 4, 5, 6];
    console.log(a.join('|'));
}
 
func();


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.

JavaScript




function func() {
    let a = [1, 2, 3, 4, 5, 6];
    console.log(a.join());
}
 
func();


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

JavaScript




function func() {
    let a = [1, 2, 3, 4, 5, 6];
    console.log(a.join(''));
}
func();


Output

123456

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

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

  • Google Chrome 5.0
  • Microsoft Edge 12
  • Mozilla Firefox 4.0
  • Safari 5.0
  • Opera 10.5

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript



Last Updated : 07 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads