How to convert an array to string in Javascript

How to convert an array to string in Javascript

In this post we will learn how to convert an array to string in JavaScript and return the result with and without commas.

In JavaScript we get two method to convert an array into a string : toString() and join() method.

Array to String using toString() method

The toString() is a in-built JavaScript method that operates on a given array and return the text inside the array as a string.


const array = ['one','two','three']

//Output: "one,two,three"

This method converts the array and return the string with commas as the separator between the elements.

You can also convert mix array to string. Here is an example with an mix array.

const array = ['one','two','three', 4, 5, 6]

//output : "one,two,three,4,5,6"

The toString() method convert each element of different data-type into a string.

JavaScript array to string using join()

If you don't want to convert the array to string with the comma separator then you can use the join() method.

The join() method returns an array as string in JavaScript. The join() method excepts a parameter , which can be serve as a separator for the output string.

Just pass the required separator as a parameter in join().


const array = ['one','two','three']
//output : "one+two+three"
//output : "one/two/three"
console.log(array.join(' '))
//output : "one two three"

Now, if you pass an empty string as a separator then it will return a string without any comma separator.

const array = ['one','two','three']

//output: onetwothree

That's it , this two method turns an array to string in JavaScript.

Related Topics:

How To Convert An Array To Object In Javascript?

How To Convert An Array Of Objects To A Single Object In JavaScript ?

Convert array to string with brackets and quotes in JavaScript

Convert String To Float In JavaScript

Convert Date To Long Date Format Using Javascript

Capitalize First Letter Of A String In JavaScript