JavaScript Array join
Method (Live Playground)
The JavaScript Array join
method creates a new string by concatenating all elements of the array separated by commas or a specified separator string.
Here is a simple example of how to use the join
method:
let fruits = ['apple', 'banana', 'cherry'];
let result = fruits.join();
console.log(result);
// Expected output:
// 'apple,banana,cherry'
In the example above, join
concatenates all the elements of the fruits
array into a single string separated by commas.
Live Playground, Try it Yourself
You can specify a different separator like this:
let fruits = ['apple', 'banana', 'cherry'];
let result = fruits.join(' and ');
console.log(result);
// Expected output:
// 'apple and banana and cherry'
In this case, fruits.join(' and ')
concatenates the elements of the fruits
array into a single string, where each element is separated by the string ' and '
.
Live Playground, Try it Yourself
The join
method can be incredibly useful when you need to create a string from an array of elements.