.join()

Elements of an array are converted to strings and concatenated together, returning the resulting string.

Syntax

array.join(seperator);

An optional separator parameter specifies a string to separate each pair of adjacent element of the array.

array.join(' + ');

If a separator is not provided, the array element will be separated with a comma.

array.join();

If the separator is an empty string, the result will be the joining of all the elements without any characters in between them.

array.join('');

Examples

Joining an array into a string without a separator argument:

const gameObjects = ['rock', 'paper', 'scissors'];
const joinNoSeparator = gameObjects.join();
console.log(joinNoSeparator);
// Output: rock,paper,scissors

Joining an array into a string with a separator argument:

const gameObjects = ['rock', 'paper', 'scissors'];
const joinWithSeparator = gameObjects.join(' + ');
console.log(joinWithSeparator);
// Output: rock + paper + scissors

Joining an array into a string with an empty string separator argument:

const gameObjects = ['rock', 'paper', 'scissors'];
const joinWithEmptyString = gameObjects.join('');
console.log(joinWithEmptyString);
// Output: rockpaperscissors
Edit this page on GitHub

Contributors

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

Learn JavaScript on Codecademy

Edit this page on GitHub

Contributors