What does the second => mean?

const sortSpeciesByTeeth = arr => arr.sort((speciesObj1, speciesObj2) => speciesObj1.numTeeth > speciesObj2.numTeeth)

I don’t understand what the second => means. From everything we’ve learned so far, => is only used once at the beginning of arrow functions. But now it seems the ‘fat arrow’ is somehow used in another way that I don’t think was ever shared in the lessons. Can someone explain?

=> speciesObj1.numTeeth > speciesObj2.numTeeth

It’s not clear to me how this second part fits in with the first part that uses sort. Maybe if I knew how the second arrow works it would make more sense?

1 Like

The second fat arrow is another function. The sort method takes a sorting function that lets us sort numbers correctly by magnitude, not just first digit the way JS normally sorts.

The outer function returns undefined since Array.sort() is an in place operation that doesn’t return anything.

Aside

This style of writing is actually discouraged in style guides. Better if the outer function is written in standard form rather than expression form.

const sortSpeciesByTeeth = function (arr) {
    arr.sort((a, b) => a.numTeeth > b.numTeeth)
}
1 Like

I agree its not clear on a site where clarity is sought. There are good articles on Dev.io that explain callbacks in better detail. I found this one helpful Understanding Callbacks - DEV Community

1 Like

I convert all the arrow function exercises to defined functions from name = argument => function body to function name(argument) {function body} I dont find that arrow functions very readable for a newbie. I dump that syntax whenever I can.