Title Case a Sentence challenge - use of `val`


Can anyone please explain how val is being used?

map() is using an anonymous function expression with val as its parameter. I see val also referenced inside the function. I don’t quite understand how val is being used as there is no argument being passed to the parameter val.

function titleCase(str) {
      var convertToArray = str.toLowerCase().split(" ");
      var result = convertToArray.map(function(val){
          return val.replace(val.charAt(0), val.charAt(0).toUpperCase());
      return result.join(" ");

    titleCase("I'm a little tea pot");

If you have an array with the contents of:

arr = [2,4,6,8,10];

and you use the .map function, the .map function will iterate through each element of your array and assign that value to the variable inside the parenthesis, in this case ‘val’ . Then you’re free to do what you want on that particular element of your array, referenced as ‘val’

think of it as

val = 2
val = 4
val = 6

If you called it function(myNumber), then you would use ‘myNumber’ in your statements.

owel, thanks for the help!