JavaScript

How to Convert Arrays to String in JavaScript?

Strings and arrays are two different variable types in JavaScript and we often need to convert arrays into a string for fulfilling our desired tasks in programming. For some specific functions, arrays are converted into strings. Therefore, in JavaScript, a defined method is used for the conversion of arrays into a string.

JavaScript allows returning the values of an array into a string by using the “toString()” method. In this article, the conversion of arrays into a string is discussed.

toString() Method

toString() is a built-in single method that is used for the conversion of arrays into strings. It converts different types of arrays into strings such as

  • Convert arrays into a string
  • Convert mix arrays into a string
  • Convert nested arrays into a String
  • Convert a nested array of objects into a string

The given example shows the conversion of an array into a string.

How to convert an array of numbers into a string

In this example, we can see that after the use of a function every element of an array is separated by commas and returned as a string.

const newArray = [3, 2, 8];
newArray.toString();  // expected output: 3,2,8

How to convert an array of strings into a string

In the second example, let’s take the array of strings and pass it to the toString() method. This example concatenates all strings in an array together by using a single comma-delimited string, and returns the array values in form of string.

const strArray= ['a', 'b', 'c'];
strArray.toString();  // expected output: a,b,c

Conversion of an array with different datatypes into a string

In the previous two examples, you learn to convert arrays of numbers and strings into strings separately. Arrays may contain mixed data types as well, which means an array contains both numbers and strings in one array. Therefore, in the next example, we learn how to convert an array with different datatypes into a string.

Example

const mixArray = ['15', 22, 'Mark'];
mixArray.toString(); // expected output: 15,22,Mark

The above example uses the toString() method to convert a an array with different datatypes into strings.

How to convert Nested Arrays to String

JavaScript handles nested arrays in an interesting manner. First, look at the following code that shows an array with nested elements.

const arrInArr = [ '15', 22, [ 'Mark', 4 ] ];
arrInArr.toString(); // expected output: 15,22,Mark,4

The elements of an array will be flattened when the toString() method is called in an array.

The resultant string consists of all the elements of the original array associated with all elements of the nested array. The toString() method separates each element of the array using a comma.

How to convert a nested array of objects into a string

Now the question is that, what if you are working with a nested array of objects?

The behavior of function will be changed in the case of objects. It can be better to show it by an example. Take a look at the following example:

const objInArr = ['15', 22, { name: 'Mark', age: 40 } ];
objInArr.toString() ;  // expected output: 5,32,[object Object]

You can see that an array of nested objects result in a string value after using the toString() function. The resulting expression shows the nested array values as [object, object]. It is due to the type of underlying objects.

Conclusion

The toString() method is used for the conversion of array to string javascript. The toString() method takes array values and returns a combined single string as a result. In this article, we learned how to convert different types of arrays into a string along with detailed examples. toString() method is used to convert: an array of numbers, strings, mixed arrays, arrays of objects, and nested arrays into strings.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.