Convert an array to a string
suggest changeUse join() to concatenate array elements with a separator:
const letters = ["a", "b", "c"];
console.log(letters.join(",")); // "a,b,c"
Transform values with map() before joining them. This separates the transformation from the string construction and handles empty arrays correctly:
const letters = ["a", "b", "c", "d"];
const alternatingCase = letters
.map((letter, index) => (
index % 2 === 0 ? letter.toLowerCase() : letter.toUpperCase()
))
.join(",");
console.log(alternatingCase); // "a,B,c,D"
join() converts undefined, null, and empty array slots to empty strings. Map values explicitly first if those cases need distinct output.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents