5 First-class functions for the novice: definitions and arguments

 

This chapter covers

  • The ways to define a function
  • What makes JavaScript functions so powerful
  • How TypeScript can catch bad function calls before they happen

Upon turning to this part of the book dedicated to JavaScript fundamentals, you might be surprised to see that the first topic of discussion is functions rather than objects. We’ll certainly be paying plenty of attention to objects in part 3 of the book, but when it comes down to brass tacks, the main difference between writing JavaScript code like the average Jill (or Joe) and writing it like a JavaScript ninja is understanding JavaScript as a functional language. The level of sophistication of all the code you’ll ever write in JavaScript hinges on this realization.

If you’re reading this book, you’re not a rank beginner. We’re assuming that you know enough object fundamentals to get by (and we’ll be taking a look at more advanced object concepts in chapter 9), but really understanding functions in JavaScript is the single most important weapon you can wield. So important, in fact, that this and the following three chapters are devoted to thoroughly understanding functions in JavaScript.

5.1 Defining functions

5.1.1 Function declarations and function expressions

5.1.2 Arrow functions

5.2 The versatility of JavaScript functions

5.2.1 Functions as first-class objects

5.2.2 Callback functions

5.3 Fun with functions

5.3.1 Storing functions in a collection

5.3.2 Attaching metadata to functions

5.3.3 Memoizing functions

5.4 Arguments and function parameters

5.4.1 Rest parameters

5.4.2 Default parameters

5.5 Declaring functions in TypeScript

5.5.1 Defining parameter types

5.5.2 Defining return types in TypeScript

5.5.3 Function overloading

5.5.4 Documenting functions with TSDoc

5.6 Summary