How to Convert an Array to a String in JavaScript

Sometimes is it necessary to convert an array to a string in JavaScript. It is possible to do this in two different ways, both of which we will learn how to use in this tutorial.

 

The toString() Method

The easiest what to turn an array into a string is with the toString() method which will return a representation of the array in a string format.

 

var fruit = ['apple', 'strawberry', 'orange']

result = fruit.toString()

console.log(result)
console.log(typeof(result))
apple,strawberry,orange
string

 

The join() Method

The join() method can also be used to convert an array into a string except you can also set any separate you want.

 

Using .join with no separator:

var fruit = ['apple', 'strawberry', 'orange']

result = fruit.join()

console.log(result)
console.log(typeof(result))
apple,strawberry,orange
script.js:7 string

 

Joining an array with an empty string as the separator:

var arr = ['h', 'e', 'l', 'l', 'o']

result = arr.join('')

console.log(result)
hello

 

Joining an array with a custom separator:

var fruit = ['apple', 'strawberry', 'orange']

result = fruit.join(' | ')

console.log(result)
apple | strawberry | orange

 

Conclusion

You now know how to change an array into a string in two different ways in JavaScript.

array string convert