Join Method in Javascript
Learn what join method in javascript with examples.
Join() Method:
It returns a new array by joining the list of elements with the given delimiter/separator.
Syntax:
join(delimeter:*): Array
where:
delimiter: indicates delimiter/ separator to be used while joining.
Note:
The join() method does not affect the original array.
Example
var fruits = ["Apple", "Orange", "banana", "Grapes", "Watermelon"];
document.write(fruits, "<br/>");
document.write(fruits.join("-"))
Output:
Steps:
- In the above code, we have created an array of fruits and assigned some values inside the square brackets.
- Then we printed the array “fruits” without using the join() method, So the default delimiter is a comma (,) displayed.
- If you want to add your desired delimiter symbol between the array list of values. Then you should use the join() method in JS and give value as your delimiters symbol.
- Here, we give the “-” hyphen symbol, so the hyphen is displayed between all array values.