JavaScript(JS) array method - join

The join() method is an array method in JavaScript that joins all elements of an array into a string. It concatenates the elements of the array together, separated by a specified separator (or a comma by default), and returns the resulting string.

Here's the syntax of the join() method:

r‮ refe‬to:theitroad.com
array.join([separator]);
  • array is the array that the join() method is called on.
  • separator is the string used to separate the elements of the array. It is an optional parameter, and if not specified, a comma is used as the separator.

The join() method returns a string containing the concatenated elements of the array.

Here's an example of how to use the join() method:

let array = ['apple', 'banana', 'orange'];

console.log(array.join()); // 'apple,banana,orange'
console.log(array.join('-')); // 'apple-banana-orange'

In this example, we have an array array containing the elements ['apple', 'banana', 'orange']. We use the join() method to join the elements of the array into a string, using a comma as the separator by default and a hyphen as the separator in the second example. The output of this code will be:

'apple,banana,orange'
'apple-banana-orange'

Note that the join() method does not change the original array. It only returns a string containing the concatenated elements of the array.