Does it matter if I use ES6 arrow functions for the event handler callback function?

Question

Does it matter if I use ES6 arrow functions for the event handler callback function?

Answer

We can use both function expressions (usually anonymous functions) and arrow functions for our event handler callback functions. Some things to keep in mind when deciding which to use:

  • Am I concerned with the value of this inside my function? If no, either an arrow function or function expression will work - we can use an arrow function if it will not make our code harder to read and understand, or if we want to make use of one of the up-to-date features of JavaScript.
  • Do I need this bound to the context in which this is called? If yes, we want to use a function expression.
  • Do I need this bound to the context in which the function is called? If yes, we want to use an arrow function.
9 Likes