I want to get test2 as String but I got only "1" instead of "1, 2, 3".
let test = [1, 2, 3];
console.log(String(test));
let test2 = new Set([1, 2, 3]);
console.log(String(...test2));
I want to get test2 as String but I got only "1" instead of "1, 2, 3".
let test = [1, 2, 3];
console.log(String(test));
let test2 = new Set([1, 2, 3]);
console.log(String(...test2));
String() converts an array to a string.
You were not converting Set into an array correctly, you just needed square brackets with the spread operator.
let test2 = new Set([1, 2, 3, 3]);
console.log(String([...test2]));
String(...test2) spreads out the elements from the set into discrete arguments to String. With your example set, it's the same as doing String(1, 2, 3). The String function doesn't do anything with any arguments you pass it other than the first one, so String(1, 2, 3) gives you the same result as String(1): It converts the argument to string, returning "1".
If you want to get, say, "1, 2, 3", you could spread the contents of the set into an array and use join:
let test2 = new Set([1, 2, 3]);
console.log(
[...test2].join(", ")
);