How to convert an array to a string in JavaScript

To convert an array to a string in JavaScript:

  1. Call the Array.join() method on the array.
  2. Pass in an optional separator. The default separator is the comma (,).
  3. The Array.join() method returns array values joined by the separator as a string
const fruits = ['Apple', 'Orange', 'Mango', 'Cherry']

const str = fruits.join()

console.log(str)

// Apple,Orange,Mango,Cherry

You can also specify a custom separator — dashes, spaces, or empty strings — as a parameter to the Array.join() method. Let us say that you want to add a space after the comma, pass the following to Array.join():

const str = fruits.join(', ')

// Apple, Orange, Mango, Cherry

Want to have each array element on its own line? Just pass in a newline character:

const str = fruits.join('\n')

// Apple
// Orange
// Mango
// Cherry

Prefer to use the HTML line break?

const str = fruits.join('<br>')

The Array.join() method works in all modern browsers and back to at least IE6.

Read this guide to learn more about JavaScript arrays and how to store multiple pieces of information in a single variable.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.