# forEach is/isn't setting

**URL:** <https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 27, 2021, 2:28pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399 "2021-09-27T14:28:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 2:28pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/1 "2021-09-27T14:28:02Z")

</div>

the console log 1 shows as expected (2D array, second element inner arrays numbers), and the console.log 2 displays as expected with iterations finally setting all numbers to 0.  
then console log 3 shows undefined.  
i’m tiring and cant work out why?

```auto

var change=cid

  console.log('1:',change)

  change=change.forEach(function(elem){

    elem[1]=0

    console.log('2:',change)

  })

  console.log('3:',change)

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 2:30pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/2 "2021-09-27T14:30:30Z")

</div>

That really doesn’t look like an intended way to use a forEach. What end result are you trying to get?

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 2:32pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/3 "2021-09-27T14:32:57Z")

</div>

i was just trying to set all the numbers to 0  
(2nd element of each inner array)  
(i could do it with a for loop but i thought this was supposed to be basically the same)

i actually just wanted to do this:

change=cid.forEach(elem=\>elem[1]=0)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 2:38pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/4 "2021-09-27T14:38:59Z")

</div>

Why not use a map?

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 2:41pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/5 "2021-09-27T14:41:13Z")

</div>

i’ll have a look now.  
but i will still be left wondering about my forEach mind anomoly

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 2:42pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/6 "2021-09-27T14:42:04Z")

</div>

sorry, do you mean .map or build an array map of variables?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 2:48pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/7 "2021-09-27T14:48:00Z")

</div>

You provided very little code, so it is hard to understand what you are trying to do.

In general, changing an object or arrays you iterate over it with array methods. For every correct way to do this, there are dozens of wrong ways to do this.

So what are you trying to do? What do you start with and what do you want to end up with?

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 2:53pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/8 "2021-09-27T14:53:27Z")

</div>

its the cash register one

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register)**
>
> Learn to Code — For Free

i’m building the ‘change’ array from the ‘cid’ array but need to set the values to zero. just to have the denominations in a 2D array with 0 quantity for each.

```auto
function checkCashRegister(price, cash, cid) {

  //var change=[["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

  var change=cid

  console.log('1:',change)

  change=cid.map(function(elem){

    elem[1]=0

    console.log('2:',change)

  })

  console.log('3:',change)

  ....

  
  change=( change.filter((elem)=>elem[1]>0) ).reverse()

  ....

  return {status: "OPEN", change: change}

}

console.log(

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

)

```

(i already have a working function but i’m trying to change things and more concerned about learning rather than completing the exercise)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 2:55pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/9 "2021-09-27T14:55:40Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 3:01pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/10 "2021-09-27T15:01:01Z")

</div>

Right, so making a new array from an old array is exactly the place where you should use a `.map()` instead of a `.forEach()`.

There is a few reasons for this

1. As a general rule, you don’t want to change the input variables unless your function description explicitly says that it does this.

2. [You are trying to capture the return value, but for a `.forEach()` the return value is `undefined`.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#return_value)

1. [`.map()` is the method for creating a new array from an old array.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

* * *

Side Note: You should never use `var`. You should use `const` for almost everything. You should use `let` for primitive variables you want to change.

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 3:13pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/11 "2021-09-27T15:13:48Z")

</div>

so it seems to work as:

```auto
  let change=cid.map(elem=>[elem[0],0])

```

i wasn’t expressing the function properly as well as using wrong one!  
lol

thanks

can you confirm ‘let’ is right choice or ‘const’?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [September 27, 2021, 7:07pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/12 "2021-09-27T19:07:56Z")

</div>

use always const, if you get an error because you are trying to change something declared with const, then change it to let

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [September 27, 2021, 7:15pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/13 "2021-09-27T19:15:56Z")

</div>

It’s fairly unlikely you will want to reassign the value of a variable that contains an array or object. More likely you will use some non-mutating method on it and return a new copy. Reassignment should really be kept to a minimum and if you do need such a variable it should be pretty obvious.

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 10:00pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/14 "2021-09-27T22:00:03Z")

</div>

this variable certainly changed in my code. are you guys saying good practice is to use more variables in order to leave things unmutated?  
i think i have to leave change as a changable ‘let’ in cash register( pun unintended). but i’d like to know the etiquette…we are supposed to use const at the expense of extra variables? i have been using ‘let’ a lot and updating variables/assignments.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 10:01pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/15 "2021-09-27T22:01:36Z")

</div>

Did the variable change which array it was pointed at, or did the contents of the array change?

```javascript
const someArray = [0, 1, 2];
someArray[1] = 42; // This is fine

```

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 10:04pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/16 "2021-09-27T22:04:58Z")

</div>

no the array changed as it is used as a return array so is the central point of the function

no…wait…

you’re right, but all the elements changed in 2 loops/function calls, and it was reversed. so i guess it’s as you suggested…it was just elemental changes?  
(i’m trying not to post project code on forum, i think you guessed right tho)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 10:11pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/17 "2021-09-27T22:11:27Z")

</div>

My rule of thumb is to always use `const` and then if you decide I need to change the [_primitive_ value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive) stored in the variable, then swap to `let`.

Arrays can be a bit tricky if you aren’t familiar with pointers. Basically, a JavaScript variable can only hold one _thing_. There is more than one piece of information needed to make an array or object, so a variable that holds an array or object actually is telling your browser where in your computer’s memory the full array or object has been stored.

So, you should use `const` for arrays and objects _unless_ you what to change which array/object your variable is talking about.

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 10:21pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/18 "2021-09-27T22:21:49Z")

</div>

```auto
let change=arrayA.map(elem=>[elem[0],0])
change[i][1]=change[i][1]+arrayB[i]
change=( change.filter((elem)=>elem[1]>0) ).reverse()

```

are these changes reasonable? or shud have another variable name assigned?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2021, 10:23pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/19 "2021-09-27T22:23:58Z")

</div>

> [@danCamalMan](#):
>
> `change[i][1]=change[i][1]+arrayB[i]`

This works fine with `const`.

> [@danCamalMan](#):
>
> `change=( change.filter((elem)=>elem[1]>0) ).reverse()`

This is making a new array, so I would call it something new. This should work fine without the extra set of `()`s, by the way.

---

<div class="post-metadata">

**Author:** ![danCamalMan](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@danCamalMan](https://forum.freecodecamp.org/u/danCamalMan)\
**Post date:** [September 27, 2021, 10:32pm UTC](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399/20 "2021-09-27T22:32:28Z")

</div>

sorted, thanks people.  
(might be about time for another paypal to FCC tomorrow)

[Next page](https://forum.freecodecamp.org/t/foreach-is-isnt-setting/479399.md?page=2)
