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

213
Views
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 answers
Answer question

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 Report

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