.join()

The .join() method returns a string representation of array elements concatenated together.

Syntax

array.join(separator);

An optional separator parameter specifies a string or character that goes between each array element in the returned string. Passing an empty string ('') will join all the elements without any characters between them. If a separator is not provided, the array elements will be separated with a comma.

Examples

Joining an array into a string without a separator argument:

const gameObjects = ['rock', 'paper', 'scissors'];
const joinNoSeparator = gameObjects.join();
const joinWithSeparator = gameObjects.join(' + ');
console.log('No separator: ', joinNoSeparator);
console.log('With separator: ', joinWithSeparator);

This produces the following output:

No separator: rock,paper,scissors
With separator: rock + paper + scissors

Codebyte Example

The following example joins an array into a string with an empty string separator argument:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn JavaScript on Codecademy