How to Convert a Set to a String in JavaScript

To convert a Set to String in JavaScript, “convert a Set to an array and then call the join() method on that array.”

Set to a String in JavaScript

Here’s a step-by-step breakdown of how to convert a set to a string:

  1. Convert the Set to an array using Array.from() method.
  2. Use the Array.join() method on the resulting array to concatenate its elements into a string.
const main_set = new Set(['a', 'b', 'c']);

// Convert the Set to an array and 
// Then join its elements into a string
const str = Array.from(main_set).join(', ');

console.log(str);

Output

a, b, c

Use the spread operator (…).

const main_set = new Set(['a', 'b', 'c']);

// Convert the Set to an array and 
// Then join its elements into a string
const str = [...main_set].join(' ');

console.log(str);

Output

a b c

You can change the delimiter inside join() to any other string if you want a different separator between elements.

Related posts

Date to String in JavaScript

Object to String in JavaScript

URL to String in JavaScript