.join()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jun 21, 2021Updated Oct 8, 2022
Contribute to Docs

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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy