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

209
Views
Unique characters in JavaScript

function unique_char(string) {
  return String.prototype.concat(...new Set(string));
}

console.log(unique_char("Hellooooooooooooo"));

Can anyone help me understand how .concat a new Set will return unique characters ? I thought .concat will add , and why is there a spread operating ... before new Set?

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

0

1)

new Set(string)

As strings in JS are iterable so Set will take string as an input and gives you set of unique characters.

2)

String.prototype.concat(...new Set(string)

The concat() method concatenates the string arguments to the calling string and returns a new string.. One or more strings to concatenate to str. - MDN

So concat will take character by character and append it into a single string

Have a look at the output of the following function

function unique_char(string) {
  const set = new Set(string);
  console.log(...set);

  return String.prototype.concat(...set);
}

console.log(unique_char("Hellooooooooooooo"));

about 4 years ago · Juan Pablo Isaza Report

0

So lets cut this command in the single pices:

  1. new Set(string) => this will create a new Set from the string and leave every char only once
  2. the ... is used to "convert" the Set to an array (the set is iterable)
  3. the String.prototype.concat is used to concat all characters from the array that was returned by the spread operator.
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!