Functions

The unit you will write most often. Four ways to declare one and what each does at parse time, how arguments really arrive, what comes back when you forget to return, and the three ideas that turn functions into building blocks: higher order use, recursion and purity.

What you will be able to do

  • Choose between a declaration, an expression and an arrow, and say what hoists
  • Use defaults, rest parameters and destructured options without surprises
  • Explain why n => { n * n } returns undefined
  • Say what an arrow does not have, and when that disqualifies it
  • Build pipe, curry and partial application from scratch
  • Write a recursive traversal with a base case, and know when a loop is better
  • Separate a pure core from the side effects at the edges

Lessons

  1. Declaring Functions — A declaration is lifted whole to the top of its scope. An expression is just a value, and a value has to be assigned before you can use it. (11 min)
  2. Parameters and Arguments — Parameters are the names you promise; arguments are the values that turn up. Defaults only fill the slots that are undefined. (12 min)
  3. Return Values — Every call produces a value. If you never say what it is, it is undefined. (9 min)
  4. Arrow Functions in Depth — An arrow has no identity of its own. No this, no arguments, no new. It borrows from where it was written. (11 min)
  5. Higher-Order Functions — A function is a value. Once you believe that, passing one, returning one and storing one in an array are all the same move. (13 min)
  6. Recursion — Trust the function with a smaller version of the problem, and stop at the version too small to shrink. (12 min)
  7. Pure Functions and Side Effects — A pure function is a formula: the same input always gives the same output, and nothing outside it can tell that it ran. (10 min)