Arrow functions

Arrow functions


Can someone explain what the arrow does here? Is “word” a new function with “word.lengh ===4” the expression?

function friend(friends){
  return friends.filter(word => word.length === 4);

friend(["Ryan", "Kieran", "Mark"]);  returns [Ryan, Mark]

word => word.length === 4

word is the variable being passed into a an anonymous arrow function. The word.length === 4 is the meat of the function. Since there is no code block (in curly brackets) it just returns the value of what is evaluated here, in this case a boolean of if the word length is 4.


The short answer is, it is the same as:

function friend(friends){
  return friends.filter(function(word) {
    return word.length === 4;

word is the anonymous callback function’s first argument (could have been named anything) which represents the current element being iterated over in the friends array. Read more about the filter method’s other arguments which can be referenced.


Thanks Kevin, that explanation helped :slight_smile:


Thanks Randell, that’s clears it up for me :slight_smile: