How do I convert array to string in JavaScript?

To convert an array to a string in JavaScript, you can use the array.toString() method. The toString() method allows you to convert and combine all the elements of an array into a single string value, where a comma will separate the array elements. If the method is called on an empty array (the array's length is zero), then an empty string will be returned in this case. Any array element that is undefined or null will be converted to an empty string. The array.toString() method does not take any parameters. If you need to convert an array to a string and use a separator between elements, you can use the array.join(separator) method. In this example of converting an array to a string in JavaScript, we use the toString() method. Click Execute to run the JavaScript toString Example online and see the result.
How do I convert array to string in JavaScript? Execute
let arr = ['JavaScript', 'array', 'to', 'string', 'example'];

console.log(arr.toString());
Updated: Viewed: 808 times

Convert array to string in JavaScript

The toString() method since JavaScript 1.8.5 (ECMAScript 5) is generic and can be used with any object. The following is the syntax for the toString() method on an array:

JavaScript toString() Syntax
array.toString()

Where:
  • array: this is an array to convert to a string
JavaScript Convert Array to String Example
let arr = ['Cherry', 'Apple', 'Strawberry', 'Orange'];

console.log(arr.toString());

// output: Cherry,Apple,Strawberry,Orange

Convert array to string with a separator between elements

To convert an array to a string using a separator between elements in JavaScript, you can use the array.join(separator) method:

JavaScript Convert Array to String with Separator Example
let arr = ['Cherry', 'Apple', 'Strawberry', 'Orange'];

console.log(arr.join('|'));

// output: Cherry|Apple|Strawberry|Orange

See also