It’s not a dumb question at all! We’re all here to learn, so never hesitate to ask if you don’t understand something.
Short answer: The .forEach()
function iterates over an array and calls a callback function once for each element of the array. As Teller mentioned, that function takes up to three parameters: the first is the element’s value, the second is its index, and the third, if included, is the array itself. The names num
and i
are arbitrary (although you should give your variables logical names, so num
and i
are good choices). The computer knows which is which by the order, not the name.
Detailed answer: As Teller said, the .forEach()
function accepts a maximum of three parameters. More precisely though, it accepts a callback function which itself accepts up to three parameters. As the name forEach imples, the callback function is called once for each element of the array on which the forEach element is called (here simply called array
). The parameters the callback accepts are, in order: the value of the element (here named num
), the index of that element in the array (here named i
), and the array the element is in (here not included since it wasn’t needed by the callback function).
Note that the names num
and i
are decided by the programmer. Which variable refers to what is determined by the order, in this case (the first is the element value, the second is the index, the third, if included, is the array). You could’ve named them anything you like, but it’s important to give variables logical names that are easy for you and anyone else reading your code to understand and remember, and it’s programming tradition to use i
for the index number.
If we take a look at the code again:
const updateUI = (array = []) => {
array.forEach((num, i) => {
const outputValueNode = document.getElementById(output-value-${i});
outputValueNode.innerText = num;
})
}
The function .forEach()
is being called on the array named array
, and it’s being passed an unnamed arrow function as a callback. It will run this callback once for each element of the array. This function takes two parameters: num
, which is the value of the element, and i
, which is the index of that element in the array. The body of the function does the following.
This line
const outputValueNode = document.getElementById(output-value-${i});
creates a constant JavaScript variable that references the HTML object with the ID #output-value-i
, where you replace “i” with the value of the parameter i. #output-value-0
for i=0
, #output-value-1
for i=1
, and so on.
This line
outputValueNode.innerText = num;
then changes the innerText of that HTML object to the value num
.
Therefore, when the .forEach()
function is done running, each one of the array.length
number of will have a value based on the value of the element at the corresponding index in the array. #output-value-0
will have an innerText equal to the value at array[0]
, #output-value-1
will have an innerText equal to array[1]
, and so on.
I hope that helps clear things up. If any further explanation is needed, please let me know.
P.S. The reason we can use the same variable name even though variables declared with const
are supposed to constant and un-editable is because the variable outputValueNode
is being declared in a for loop. If I understand correctly, the scope of the variable is therefore just that iteration of the loop, and the variable doesn’t exist outside that context, allowing the name to be used again in the next iteration.
Btw, the []
in your code is rendering as a checkbox on the forum, like this: array = . When posting a block of code to the forum, make sure to include a line of three backticks (```) before and after it to ensure it is formatted and rendered properly as code.
For example, if I put this in the editor:
It will render like this to the reader:
const exampleVariable = "example of code block";
console.log(exampleVariable);
If it’s a single line of code or a few words, use a single backtick on each side instead. It will render like this
.
This will help make your code clear to others and make it easier for them to help you. Cheers!