Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Why do I get wrong string format for javascript Set

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));

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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]));

about 4 years ago · Juan Pablo Isaza Report

0

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(", ")
);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!