Lesson 17. Arrow functions

 

After reading lesson 17, you will

  • Know how to make your code succinct with arrow functions
  • Know how to maintain context with arrow functions

Arrow functions in JavaScript are directly inspired by the fat arrow functions of CoffeeScript. They behave similarly to CoffeeScript’s by providing a much more succinct way of writing a function expression and also maintaining their context (what this refers to). The syntax isn’t always the same as CoffeeScript’s but they are just as useful and a great addition that makes things like anonymous functions and inline callbacks much more elegant.

Sometimes extraneous syntax can make code much harder for the human mind to parse, because there are several extra pieces to think about. It’s not a hard and fast rule that few characters mean easy to understand. For example, single-letter variable names or overly clever _code golf_[1] solutions are terrible to read. However, if you can convey a meaning eloquently with fewer characters, it’s almost always more understandable than it would be with more characters. The human brain has a hard time parsing lots of information at once, so the more noise you can reduce, the better.

1A game in which a programmer writes a program with as few characters as possible.

17.1. Succinct code with arrow functions

17.2. Maintaining context with arrow functions

17.3. Arrow function gotchas

Summary