Home  >  Article  >  Web Front-end  >  How to Create a Comma-Separated List from a JavaScript Array?

How to Create a Comma-Separated List from a JavaScript Array?

Barbara Streisand
Barbara StreisandOriginal
2024-10-27 14:35:29521browse

How to Create a Comma-Separated List from a JavaScript Array?

Creating Comma-Separated Lists from JavaScript Arrays

Converting a JavaScript array of strings into a comma-separated list is a common operation. Here's how you can achieve this with ease:

Using Array.prototype.join()

The Array.prototype.join() method is the most straightforward way to create a comma-separated list. It takes a separator as its argument, which by default is a comma and space.

<code class="js">var arr = ["Zero", "One", "Two"];

document.write(arr.join(", "));</code>

Result: "Zero, One, Two"

You can specify a different separator if needed, for example:

<code class="js">document.write(arr.join("; "));</code>

Result: "Zero; One; Two"

The above is the detailed content of How to Create a Comma-Separated List from a JavaScript Array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn