Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda