Converting Array to String in JavaScript

To convert an array to a string in JavaScript, you can use the Array.toString() method. This method returns a string where all array elements are concatenated into a comma-separated string. The Array.toString() method does not change the original array. Calling this method on an empty array results in an empty string. Any array elements that are null or undefined will be converted to an empty string. The Array.toString() method does not accept any parameters. If you need to convert an array to a string with a specific separator, use the Array.join(separator) method. In this JavaScript Array to String example, we use the Array.toString() method to convert an array with elements to a string. More examples of converting an array to a string are listed below. Click "Run" to run the JavaScript toString example online and see the result.
Converting Array to String in JavaScript Execute
let arr = ['JavaScript', 'array', 'to', 'string', 'example'];

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

What is JavaScript?

JavaScript is a high-level interpreted programming language primarily used in client-side web development to create interactive web pages. JavaScript supports various programming paradigms, including object-oriented, procedural, and functional programming. JavaScript can also be used to develop server-side solutions thanks to Node.js js or multi-platform desktop applications thanks to Electron.js. JavaScript has a large developer community that creates and maintains an extensive database of open-source projects. JavaScript is platform-independent and is supported by almost all modern web browsers.

What is an array in JavaScript?

An array in JavaScript is a data structure that allows you to store multiple values of the same or different data types in a single variable. In JavaScript, you can create arrays with the square brackets "[...]" or with the "new Array()" constructor. Each array element has an index, allowing you to access them directly. JavaScript provides many built-in functions for containing, reducing, reversing, merging, joining, slicing, cloning, copying, clearing, and inserting arrays. You can also get a sum of array elements and convert an array to JSON.

What is a string in JavaScript?

In JavaScript, a string is a sequence of characters enclosed in single ('...') or double ("...") quotes. A string can contain letters, numbers, symbols, spaces, and special characters. JavaScript strings can encode Unicode characters using the "\uXXXX" syntax. JavaScript provides built-in functions for containing, splitting, concatenating, trimming, and reversing strings. You can also create a multiline string, check if it starts or ends with another string, and get the length of a string.

How to convert an array to a string in JavaScript?

In JavaScript, you can convert an array to a string using the Array.join(separator) and array.toString() methods. The join() method allows you to combine all the elements of an array into one string. The join() method takes an optional argument that is used to separate the elements of the array when they are combined. If no delimiter is specified, items are concatenated with a comma by default. The toString() method converts and concatenates all the elements of an array into a single string value, where by default, the array elements are separated by a comma. You can also convert an array to a string with a for loop.

JavaScript Convert Array to String Example
const arr = ['JavaScript', 'Array', 'to', 'String'];

const str = arr.toString();

console.log(str);

// output: JavaScript,Array,to,String

JavaScript Array to String Examples

The following are examples of converting an array to a string in JavaScript:

Converting array to string using toString() method

The toString() method in JavaScript is a built-in method used to convert an object to a string representation. The toString() method since JavaScript 1.8.5 (ECMAScript 5) is generic and can be used with any object.

JavaScript Convert Array to String using toString()
const arr = [1, 2, 3, 4, 5];

console.log(arr.toString());

// output: 1,2,3,4,5

Converting a nested array to a string

The following is an example of converting a nested array to a string in JavaScript using the toString() method:

JavaScript Convert Nested Array to String
const arr = [[1, 2], [3, 4], [5, 6]];
const str = arr.toString();

console.log(str);

// output: 1,2,3,4,5,6

Converting array to string using join() method

In JavaScript, the Array.join(separator) method is used to join all elements of an array into a string. The elements are separated by a specified separator, passed as an argument to the method.

JavaScript Convert Array to String using join()
const arr = ['JavaScript', 'Array', 'to', 'String'];

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

// output: JavaScript Array to String

Converting 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
const arr = ['JavaScript', 'Array', 'to', 'String'];

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

// output: JavaScript|Array|to|String

Converting array to string with line breaks

You can convert an array to a string with line breaks (\n) by using the array.join() method and passing "\n" as the separator.

JavaScript Convert Array to String with Line Breaks
const arr = ['JavaScript', 'Array', 'to', 'String'];

console.log(arr.join('\n'));

// output: 
// JavaScript
// Array
// to
// String

Converting array to string with for loop

You can convert an array to a string with a for loop that iterates over the elements of an array and concatenates each element with a given variable.

JavaScript Convert Array to String using for loop
const arr = ['JavaScript', 'Array', 'to', 'String'];
let str = '';

for (let i = 0; i < arr.length; i++) {
  str += arr[i];
}
console.log(str);

// output: JavaScriptArraytoString

How to convert a string back to an array in JavaScript?

To convert string to array in JavaScript, you can use the string.split() method:

JavaScript String to Array Example
const str = 'JavaScript String to Array';

console.log(str.split(' '));

// output: ['JavaScript', 'String', 'to', 'Array']

See also