Arrow functions

Arrow functions
0.0 0

#1

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]

#2
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.


#3

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.


#4

Thanks Kevin, that explanation helped :slight_smile:


#5

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