# How to convert an array to a string in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published May 14, 2020, updated Oct 14, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-convert-array-to-string

> Learn how to use the join() method in vanilla JavaScript to convert an array to a string.

To convert an [array](https://attacomsian.com/blog/javascript-arrays) 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 

```javascript
const fruits = ['Apple', 'Orange', 'Mango', 'Cherry']

const str = fruits.join()

console.log(str)

// Apple,Orange,Mango,Cherry
```

You can also specify a custom separator &mdash; dashes, spaces, or empty strings &mdash; 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()`:

```javascript
const str = fruits.join(', ')

// Apple, Orange, Mango, Cherry
```

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

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

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

Prefer to use the HTML line break?

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

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

Read [this guide](https://attacomsian.com/blog/javascript-arrays) to learn more about JavaScript arrays and how to store multiple pieces of information in a single variable.
