join() – Join Elements of Array
array.join(separator)
array
is an array of elementsjoin
is the array method nameseparator
is an optional string value. default value is comma
join()
method joins the elements of the array with the given separator
string, and returns the result as a string. join()
method does not modify the original array
Examples
1. join the elements of the array fruits
with '-'
hyphen character as separator
let fruits = ['apple', 'banana', 'cherry'];
let separator = '-';
output = fruits.join(separator);
console.log(output);
2. call join()
method on fruits
array with no separator passed
let fruits = ['apple', 'banana', 'cherry'];
let output = fruits.join();
console.log(output);
since comma is the default separator, we get a comma separated elements in the resulting string