Joining JavaScript Array Elements to a String

To join JavaScript array elements in a string, you can use the array.join(separator) method. The array.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 array.join() method does not modify the 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 an empty string. In this JavaScript Join Array Elements into a String Example, we use the array.join() method to join all array elements in a string. Click Execute to run the JavaScript Array Join Example online and see the result.
Joining JavaScript Array Elements to a String Execute
let array = ['Paris', 'Madrid', 'Rome', 'London'];

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

What is an array in JavaScript?

An array is a special built-in object in JavaScript that allows storing multiple elements of different types in a single variable. A JavaScript array is created using square brackets "[...]" or using the "new Array()" constructor. Each element in an array has its own index, allowing you to access an element directly by its index or iterate through the array with a "for loop". JavaScript provides a rich set of built-in functions for joining, slicing, reversing, merging, copying, containing, cloning, inserting, clearing, and reducing arrays. You can also convert string to array, convert array to JSON, convert array to string, and get a sum of array elements.

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
array.join(separator)

Where:
  • 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'];

console.log(array.join());

// output: WhatsApp,Telegram,Viber

How to join two arrays in JavaScript?

To join two or more arrays in JavaScript, you can use the array.concat() method. The array.concat() method concatenates the provided arrays and returns a new array without alerting the existing one.

JavaScript Join Two Arrays Example
const array1 = ['Paris', 'Madrid'];
        
const array2 = ['Rome', 'London'];

const result = array1.concat(array2);

console.log(result);

// output: Paris | Madrid | Rome | London

How to join an array of strings in JavaScript?

To join an array of strings in JavaScript, you can use the array.join() method. By passing a separator to the array.join() method you can control how the resulting string is generated.

JavaScript Join Array of Strings Example
const array = ['One', 'Two', 'Three'];
        
const result = array.join('\n');

console.log(result);

// output: 
// One
// Two
// Three

How to join an array using a "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 an 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;
}

console.log(newArr);

// 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