Convert an array to a string

suggest change

Use 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents