Javascript Array join()

The join() method returns a new string by concatenating all of the elements in an array, separated by a specified separator.

Example


join() Syntax

The syntax of the join() method is:

arr.join(separator)

Here, arr is an array.


join() Parameters

The join() method takes in:

  • separator (optional) - A string to separate each pair of adjacent elements of the array. By default, it is comma ,.

join() Return Value

  • Returns a String with all the array elements joined by separator.

Notes:

  • The join() method does not change the original array.
  • Elements like undefined, null, or empty array have an empty string representation.

Example: Using join() method

Output

[ 'Terence', 28, 'Kathmandu' ]
Terence | 28 | Kathmandu
3.141592
44 and abc and 

Here, we can see that the join() method converts all the array elements into a string and separates each element by the specified separator.


Recommended Readings: