Follow The New Tutorial updates on WhatsApp, Telegram or YouTube.

JavaScript Array join() method

The join() method returns an array as a string. It combines all the elements of an array into a string and return a new string.

You can use any type of separators to separate given array elements. The default is comma (,).

The join() method does not change the original array.

In this tutorial, you will learn about the JavaScript Array join() method with the help of examples.

The arr.join() method is used to join the elements of an array into a string.

Syntax

array.join(separator)

This method accept single parameter as mentioned above and described below:

separator– Optional. The separator to be used. Default is a comma.

If the array has only one item, then that item will be returned without using the separator.

If separator is an empty string, all elements are joined without any characters in between them.

Return value

A string with all array elements joined. If arr.length is 0, the empty string is returned.

Examples

Below are the examples of the Array join() method.

const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"
var a = ['Wind', 'Water', 'Fire'];
a.join();      // 'Wind,Water,Fire'
a.join(', ');  // 'Wind, Water, Fire'
a.join(' + '); // 'Wind + Water + Fire'
a.join('');    // 'WindWaterFire'
let message = ["JavaScript", "is", "fun."];

// join all elements of array using space
let joinedMessage = message.join(" ");
console.log(joinedMessage);

// Output: JavaScript is fun.
var info = ["Terence", 28, "Kathmandu"];

var info_str = info.join(" | ");

// join() does not change the original array
console.log(info); // [ 'Terence', 28, 'Kathmandu' ]

// join() returns the string by joining with separator
console.log(info_str); // Terence | 28 | Kathmandu

// empty argument = no separator
var collection = [3, ".", 1, 4, 1, 5, 9, 2];
console.log(collection.join("")); // 3.141592

var random = [44, "abc", undefined];
console.log(random.join(" and ")); // 44 and abc and

Here, you can see that the join() method converts all the array elements into a string and separates each element by the specified separator.