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

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

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 Denunciar

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