I would like to understand more how parameters work in functions. I have googled it, but I cannot find what I am looking for. From the attached javascript code:
1)Why item
and index
are automatically linked to the actual item and index of fruit
?
2) In particular if I change item
and index
into ite
and 'ind' or any other name, js still understands that the 2 parameters are still linked to items of fruits
and their position, why?
3) Can myFunction(index, item)
be traslated into an arrow function with 2 parameters and incorporeted in .foreach(), in order to be more efficient?
Many thanks in advance.
let text = "";
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
function myFunction(item, index) {
text += index + ": " + item + " ";
}
console.log(text)
//=> 0: apple 1: orange 2: cherry