Chapter 5

Functions

So far in this tutorial, we’ve repeatedly mentioned JavaScript functions, and in this chapter we’ll finally learn how to define functions of our own. The resulting ability gives us greater flexibility as programmers, and enables powerful techniques like forEach (Section 5.4) and functional programming (Chapter 6). Functions: achievement unlocked (Figure 5.1).

images

Figure 5.1: Time to level up.

5.1 Function Definitions

As we saw in Section 1.2, function calls in JavaScript consist of a name and zero or more arguments enclosed in parentheses:

> console.log("hello, world!");
hello, world!

As discussed ...

Get Learn Enough JavaScript to Be Dangerous: Write Programs, Publish Packages, and Develop Interactive Websites with JavaScript 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.