How to Convert an Array to a String in Javascript

Tuesday, April 13, 2021 • One minute to read

to-the-pointJavascript2021javascriptto-the-point

This post will answer the below questions

  • How to convert an array to a string?
  • How to convert an array to a string with a character in between the elements?

Use join method to convert the array to a string with a separator.

Syntax

1array.join(separator(optional))

,(comma) is the default separator. Separator is a string not a character. - or namaste both are valid separator.

1const dateArray = ["Sun", 11, "Apr", 2021, "12:59:48", "GMT"];
2const dateString = dateArray.join();
3const dateStringHyphen = dateArray.join("-");
4console.log(dateString);
5console.log(dateStringHyphen);

Output

1Sun,11,Apr,2021,12:59:48,GMT
2Sun-11-Apr-2021-12:59:48-GMT

to-the-pointJavascript2021javascriptto-the-point

PreviousHow to Add and Remove a Property of an Object in Javascript

NextHow to Sort an Array of Objects in Javascript