How to Convert JavaScript Array to String

JavaScript arrays are used to save multiple values in a single variable. The typeof operator in JavaScript returns “object” for arrays. The arrays are special kinds of objects.

JavaScript Array to String

To convert array to string in JavaScript, use the toString() method. The array.toString() is a built-in JavaScript method that returns a string with all array values separated by commas. The toString() method does not modify the original array.

Syntax

array.toString()

Arguments

The toString() method does not take any argument.

Return Value

It is a String that represents the values of the array, separated by a comma.

Example

Let’s define an array using square brackets([ ]) and then use the toString() method to convert it into a string.

const netflix = ["Stranger Things", "Money Heist", "Loki", "WandaVision"]

let data = netflix.toString()

console.log(data)

Output

Stranger Things,Money Heist,Loki,WandaVision

You can see from the output that the array is converted into a string with a comma.

Converting mix arrays (consisting of both numbers and strings)

To convert a mixed array that consists of numbers and strings, use the toString() method.

const netflix = ["Stranger Things", 11, "Money Heist", 8, "Loki", 5, "WandaVision"]

let data = netflix.toString()

console.log(data)

Output

Stranger Things,11,Money Heist,8,Loki,5,WandaVision

Converting Array to String using an array.join() method

JavaScript array join() is a built-in method used to convert the elements of an array into a string. The toString() method can’t be used on the array of objects because it will return [object Object] instead of the actual values.

Syntax

array.join(separator)

Arguments

The separator is an optional argument. The separator to be used. If omitted, the elements are separated with a comma.

Return Value

It returns a string representing the array values, separated by the specified separator.

Example

Let’s pass nothing as a separator to the join() function and see the output.

const netflix = ["Stranger Things", "Money Heist", "Loki", "WandaVision"]

let data = netflix.join()

console.log(data)

Output

Stranger Things,Money Heist,Loki,WandaVision

The default argument for the join() method is “,” as you can see from this example.

Let’s pass a space as an argument to the join() function.

const netflix = ["Stranger Things", "Money Heist", "Loki", "WandaVision"]

let data = netflix.join(" ")

console.log(data)

Output

Stranger Things Money Heist Loki WandaVision

You can see that it leaves the spaces between different words.

Working with Nested Arrays

To convert a nested array to string in JavaScript, use the toString() function. When the toString() method is called on an array, the array object is flattened.

Flattened arrays consist of an array that contains all of the elements of the original array, coupled with all of the elements of the nested array, combined to appear as a single array.

The toString() method then separates all elements with a comma.

const netflix = ["Stranger Things", 11, ["Money Heist", 8], ["Loki", 5], "WandaVision"]

let data = netflix.toString()

console.log(data)

Output

Stranger Things,11,Money Heist,8,Loki,5,WandaVision

You can see that when the toString() function is called on the array, the array object is flattened.

Convert Array of Objects to String

To convert an array of objects into a string in JavaScript, use the JSON.stringify() method. The JSON.stringify() is a built-in method that converts a JavaScript object or value to a JSON string.

const netflix = [
 {
 name: "Stranger Things",
 char: "Eleven",
 power: "Telekinetic"
 }]

let data = JSON.stringify(netflix);

console.log(data)

Output

[{"name":"Stranger Things","char":"Eleven","power":"Telekinetic"}]

That is it for converting an array to a string data type in JavaScript.

Leave a Comment