How to Convert an Array to a String in Javascript
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
array.join(separator(optional))
,
(comma) is the default separator. Separator is a string not a character.-
ornamaste
both are valid separator.
const dateArray = ["Sun", 11, "Apr", 2021, "12:59:48", "GMT"];
const dateString = dateArray.join();
const dateStringHyphen = dateArray.join("-");
console.log(dateString);
console.log(dateStringHyphen);
Output
Sun,11,Apr,2021,12:59:48,GMT
Sun-11-Apr-2021-12:59:48-GMT