How do we Use Join() Method in JavaScript

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:

join method in javascript

Steps:

  1. In the above code, we have created an array of fruits and assigned some values inside the square brackets.
  2. Then we printed the array “fruits” without using the join() method, So the default delimiter is a comma (,) displayed.
  3. 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.
  4. Here, we give the “-” hyphen symbol, so the hyphen is displayed between all array values.

Leave a Reply