He is interested in what form of writing of the switch functions, from the ones below, is most optimal from the point of view of understanding and readability of the code, and from the point of view of conciseness of writing the code. So to say, what to teach yourself from the very beginning of studying JS. I already got used to writing as in v 2, but I met a version of the record v 4, which seems to be better for understanding. Give advice, please.

/ v 1 const getUsersByEyeColor = (arr, color) => arr.filter(val => val.eyeColor === color); // v 2 const getUsersByEyeColor = (arr, color) => arr.filter( val => val.eyeColor === color ); // v 3 const getUsersByEyeColor = (arr, color) => { return arr.filter(val => val.eyeColor === color); }; // v 4 const getUsersByEyeColor = (arr, color) => { const usersByEyeColor = arr.filter(user => user.eyeColor === color); return usersByEyeColor; }; console.log(getUsersByEyeColor(users, 'blue')); const users = [ { id: '701b29c3-b35d-4cf1-a5f6-8b12b29a5081', name: 'Moore Hensley', email: 'moorehensley@indexia.com', eyeColor: 'blue', phone: '+1 (848) 556-2344', friends: ['Sharron Pace'], isActive: false, balance: 2811, skills: ['ipsum', 'lorem'], gender: 'male', age: 37, }, .... ] 
  • What is the difference between the first and second options, and the third from Thursday? - Grundy
  • In the first and second minimal differences. In the fourth, in the body of the function, a function is created with the name to which the parameter with the name user is passed, which seems to be better perceived than just el (as in the previous three). The question may be strange, but I want it to be used on your projects. I have never come across programming before. - Viktor Hirenko
  • In general, all the examples are bad - Grundy

1 answer 1

All the examples do not look very good: on the one hand, the name of the variable getUsersByEyeColor , on the other, in all variants the parameter is called arr .

To make it more readable, you just need to change the names of the parameters, for example:

 const getUsersByEyeColor = (users, color) => users.filter(user => user.eyeColor === color); 

Then the meaning in the intermediate variable, as in the fourth variant, disappears, and it is immediately obvious that users are accepted, and a specific user is checked.