Confusion on arrow function syntax

Regarding the code in the challenge below, I understand how to use an arrow function, but some of the details are confusing me. If “users” is a variable that we created, why does the function also use the singular “user” and how is it distinguished from its plural form?

I read through this page on mozilla but haven’t been able to find the information I’m looking for. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Challenge: Use the map Method to Extract Data from an Array

const users = [
  { name: 'John', age: 34 },
  { name: 'Amy', age: 20 },
  { name: 'camperCat', age: 10 }
];

const names = users.map(user => user.name);
console.log(names); // [ 'John', 'Amy', 'camperCat' ]

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Use the map Method to Extract Data from an Array

Link to the challenge:

1 Like

It’s not distinguished in a way you think. user => user.name might be here as well whatever => whatever.name. In this example, user is just used as a name for the one element from the users array.

2 Likes

What @sanity said. In this line below:

user => user.name

user is just a placeholder for every single element in the array. You could call it

element => element.name

but it’s better to be descriptive and name the placeholder in a way that indicates what each element is :slight_smile: Does this help?

2 Likes

I think I am getting closer and closer…

so the placeholder could be named anything as long as the parameter and the value it returns have the same name.

users => users.name (passes)

user => user.name (passes)

x => x.name (passes)

x => y.name (fails)

1 Like

Yes! It can be anything but convention-wise, it’s good if the placeholder is descriptive of what each element is (in case you come back to your code 3 months later, or even a week later :smiley: , or someone else takes it over). In this case, the pluralized form users can be confusing because you’re working on, in fact, just one user at a time :slight_smile:

1 Like

When I’m going for tiny and terse, I often just use the first letter, e.g. users.map(u => u.foo()). What it’s operating on is still pretty obvious. In a classic loop body where things are more separated, I tend to go for the full singular word.

2 Likes