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

74
Vistas
Add an element to a const set

How would you add an element to an existing Set when defining it? For example, if I have:

const s1 = Set([1,2,3]);
const elem = 4;
const s2 = s1 + elem; // ?

Is using the spread operator (...) the correct way to do this?

s2 = new Set([...s, elem]);

Or is there another/better approach?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just use Set#add. Using const here only prevents setting s1 to another value, but does not make the Set immutable.

const s1 = new Set([1,2,3]);
const elem = 4;
s1.add(elem);
console.log([...s1]);

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