How do I join array elements into a string in JavaScript?

To join all array elements in a string in JavaScript, you can use the array.join(separator) method. The join() method takes an array as input and a delimiter as a parameter and returns a string where all array elements are joined into a string using the specified delimiter. By default, array elements are concatenated using a comma "," as a delimiter. The join() method does not modify the original array. If the method is called on an empty array (the length of the array is zero), then an empty string will be returned; any array element that has the value "undefined" or "null" will be converted to the empty string. In this JavaScript Join Array Elements into a String Example, we use the join() method to join all array elements in a string. Click Execute to run the JavaScript Array Join Example online and see the result.
How do I join array elements into a string in JavaScript? Execute
let array = ['Paris', 'Madrid', 'Rome', 'London'];

console.log(array.join(' | '));
Updated: Viewed: 2018 times

How to join array elements in JavaScript?

The array.join(separator) method in JavaScript allows you to concatenate all the elements of an array into a single string. By default, array elements will be separated by "," (comma); this behavior can be changed by passing the necessary separator as a parameter. If an array element has an undefined or null value, it will be converted to an empty string when the array elements are joined to a string.

JavaScript join() Syntax

  • separator (optional): the separator to be used when concatenating elements in a string value. If an empty string is passed as a method parameter, the array elements will be joined into one string without using a separator. The default value is comma ",".
JavaScript Array Join Example
let array = ['WhatsApp', 'Telegram', 'Viber'];


// output: WhatsApp,Telegram,Viber

How to join array using for loop in JavaScript?

To join array elements to a string, you can also use a for loop. Please note that this method is slower and takes additional memory space.

JavaScript Split String with for loop Example
let array = ['Paris', 'Madrid', 'Rome', 'London'];
let newArr = [];
let separator = ' | ';

for(let i = 0; i < array.length; i++) {
  newArr += array[i];
  if (i < array.length - 1) newArr += separator;


// output: Paris | Madrid | Rome | London

How to convert string to JavaScript array?

The string.split() method is the opposite of the array.join() method and is used to split the given string into an array of strings using the provided delimiter. The main difference between the join() and split() methods is that the join() method is used to join array elements into a string, while the split() method is used to split a string into an array.

JavaScript Split String Example
let str = 'Paris | Madrid | Rome | London';

console.log(str.split(' | '));

// output: ['Paris', 'Madrid', 'Rome', 'London']

See also