Home > Web Front-end > JS Tutorial > body text

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

Barbara Streisand
Release: 2024-10-27 14:35:29
Original
523 people have browsed it

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>
Copy after login

Result: "Zero, One, Two"

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

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template