How to Use the Array join() Method in JavaScript

01/28/2022

Contents

In this article, you will learn how to use the array join() method in JavaScript.

The join() method

You can use the join() method to join each element in the array to create a new string.

The syntax is below.

array.join()
//or
array.join(separator)

array.join()

If no argument is specified, a comma is specified as the separator.

Below is a sample.

const array = ["Apple", "Banana"];
console.log(array.join());
//"Apple,Banana"

array.join(separator)

If you specify a separator, all elements in the array are joined with the specified separator.

Below is a sample.

const array = ["Apple", "Banana", "Lemon"];

console.log(array.join(""));
//"AppleBananaLemon"

console.log(array.join(" "));
//"Apple Banana Lemon"

 console.log(array.join(", "));
//"Apple, Banana, Lemon"

console.log(array.join(" + "));
//"Apple + Banana + Lemon"