Convert Array To String In JavaScript

Convert Array To String In JavaScript | JavaScript uses the array to store numerous elements in a single variable. The succession of characters is stored on the string. We frequently get into situations when we need to change an array in JavaScript into a string. JavaScript has a function called toString() that is employed for this.

Convert Array To String In JavaScript Using toString()

JavaScript’s toString() method may be used to turn an array into a string by simply applying it to the array that has been provided. This produces an array that has been stringified. Each element is internally converted to a string using JavaScript before being concreted to build the output string.

How to change an array of numbers into a string? In this illustration, we can see that each element of an array is divided by commas and returned as a string following the application of a function.

const newArray = [1, 5, 9];
var string = newArray.toString();
console.log("Array: " + string);


Array: 1,5,9

How to change a string from an array of strings? Let’s use the array of strings from the second example and send it to the toString() method. In this example, all the strings in an array are concatenated using a single comma-delimited string, and the results are returned as strings.

const strArray = ["a", "b", "c"];
var string = strArray.toString();
console.log("Array: " + string);


Array: a,b,c

Converting an array of various datatypes into a string:- You learned how to independently transform arrays of numbers and strings into strings in the first two instances. Arrays may also hold mixed data types, meaning one array may contain both strings and numbers. As a result, in the next example, we will learn how to turn an array of several datatypes into a string.

const mixArray = ["34", 23, "Alex"];
var string = mixArray.toString();
console.log("Array: " + string);


Array: 34,23,Alex

Javascript arrays are converted to strings using the toString() method. The toString() process combines array values into a single string. With the help of thorough examples, we learned how to turn various array types into strings in this tutorial. An array of numbers, strings, mixed arrays, arrays of objects, and nested arrays can all be converted into strings using the toString() method. Also see:- Find the Longest String in an Array in JavaScript

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *