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

210
Vistas
I don't know where forEach() works

Guys i am sorry to ask this. Maybe a easy one but i am new at this language.

let arr =[1,2,3,4,5,6];
arr.forEach(a => a++);
console.log(arr);

why doesn't this work?

let arr =[
{name:"a"},
{name:"b"},
{name:"c"}
];
arr.forEach(a => a.name = "d");
console.log(arr);

but this works?

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

0

The value passed to the .forEach callback is either passed by value (if it's a primitive type), or effectively as a reference (if it's an object or array).

Your primitive a++ therefore is modifying a copy of the value, and so does nothing useful. When you pass an object, though, it's possible to modify the properties of that object, because the (copy of the) reference you're now holding in a still refers to the original object.

about 4 years ago · Juan Pablo Isaza Denunciar

0

For each is just a function that loops over the array, by doing a => a++, you are not checking for any condition or having any logic there. From my understanding what you looking for in .map function, which loops over an array and returns another one

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