While working with javascript arrays, often there is a requirement to convert an array to a string. This article will discuss ways to convert arrays to string with comma.

Table of Contents:

Convert array to string using join()

Javascript’s join(separator) method joins the elements of an array into a string divided by the separator passed in the argument.

Example:-

Convert the array [“Javascript”, “Is”, “Popular”,”Language”] to string

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let finalString  = stringArray.join(",");
console.log(finalString);

Output:-

Javascript,Is,Popular,Language

Explanation:-

Here, the join() method is used, passing in the comma(‘,’) as a separator to convert the array to a string value—this string value is stored in finalString and printed on the console.

Convert array to string using + Operator

Javascript’s concatenation operator(+) chains two string values together, returning another string that is the union of the two operands.

Example:-

Convert the array [“Javascript”, “Is”, “Popular”,”Language”] to string

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let finalString = stringArray + ""; 
console.log(finalString);

Output:-

Javascript,Is,Popular,Language

Explanation:-

  • Here, the two operands of concatenation operator(+) -> “Javascript”, “Is”, “Popular”,”Language” + “”.
  • Since the stringArray is concatenated with blank string (“”) the array gets converted to string.

Convert array to string using toString()

Javascript’s toString() method returns a string with all the elements of the specified array.

Example:-

Convert the array [“Javascript”, “Is”, “Popular”,”Language”] to string

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let finalString = stringArray.toString();
console.log(finalString);

Output:-

Javascript,Is,Popular,Language

Convert array to string using concat()

Javascript’s concat() method joins the arguments to the calling string.

Example:-

Convert the array [“Javascript”, “Is”, “Popular”,”Language”] to string

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let finalString  = "";
finalString = finalString.concat(stringArray);
console.log(finalString);

Output:-

Javascript,Is,Popular,Language

Explanation:-

  • The concat() method will concatenate the two arguments to a single string. Since the caller of the function is string, the final result will be a string.

Read More:

I hope this article helped you to convert a javascript array to a string. Good Luck !!!