JavaScript Array join() Method
The JavaScript Array 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 accepts a single parameter as mentioned above and described below:
- separator: It is Optional i.e., it can be either used as a parameter or not. Its default value is a comma(, ).
Example 1: In this example, the function join() joins together the elements of the array into a string using ‘|’.
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.
let a = [1, 2, 3, 4, 5, 6];
console.log(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).
let a = [1, 2, 3, 4, 5, 6];
console.log(a.join(''));
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:
JavaScript Array join() Method- FAQs
What does the Array join() method do in JavaScript?
The join() method in JavaScript creates and returns a new string by concatenating all the elements in an array, separated by a specified separator string. If no separator is provided, a comma is used by default.
How does the join() method work?
The join() method concatenates all elements of an array into a single string. You can specify a separator to be placed between each element in the resulting string. If no separator is specified, the elements are separated by commas.
What happens if I omit the separator parameter?
If the separator parameter is omitted, the join() method uses a comma (,) as the default separator.
Can I use an empty string as a separator?
Yes, you can use an empty string as a separator to join array elements without any characters in between.
What happens if the array contains null or undefined elements?
If the array contains null or undefined elements, they are converted to empty strings in the resulting string.


