.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,scissorsWith separator: rock + paper + scissors
Codebyte Example
The following example joins an array into a string with an empty string separator argument:
All contributors
- garanews209 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- Anonymous contributorAnonymous contributor88 total contributions
- garanews
- christian.dinh
- Anonymous contributor
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.