chapter 3

Functions

A function is an object that carries out a specific computation; it is written once but can be run over and over again. This chapter illustrates how to define and use functions, spending some time on one of JavaScript's most useful and powerful features: the ability to pass functions as arguments to other functions.

3.1 Function Definitions and Function Calls

Conceptually, a function transforms inputs to outputs. Figure 3.1 shows the “computation” of an account balance after t years, given a starting balance p, and an annual percentage rate r, compounded n times per year.

In JavaScript, a function consists of a block of executable code, called its body, together with zero or more “inputs,” called parameters. The following ...

Get The JavaScript Programming Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.