Iterate over Arrays with map-Need help in explanation

Iterate over Arrays with map-Need help in explanation
0

#1

I need some explanation on exercise Iterate over Arrays with map. The example code given in exercise is written below

var oldArray = [1, 2, 3];
var timesFour = oldArray.map(function(val){
return val * 4;
});
console.log(timesFour); // returns [4, 8, 12]
console.log(oldArray); // returns [1, 2, 3]

My Question is what exactly val parameter role is here. I am unable to understand through the explanation given in the exercise


#2

The name val is just a variable name. It could have been named anything (i.e. myVal, yourVal, etc…). What is important is that the anonymous callback function takes up to 3 arguments whose order dictate what each argument represents. The first argument refers to the actual element during each iteration of the array map was called on. In this case, the first argument is val. If there was a second argument, then it would refer to the index of the current element in the iteration. If there was a third argument, it would refer to the array that map was original called on. Most of the time, only the first and second arguments are used in solutions.


#3

Thanks randelldawason. Your explanation is too easy for understanding