O'Reilly logo

Head First JavaScript Programming by Elisabeth Robson, Eric T. Freeman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10. Liberated functions

First Class Functions

image with no caption

Know functions, then rock. Every art, craft, and discipline has a key principle that separates the intermediate players from the rock star virtuosos—when it comes to JavaScript, it’s truly understanding functions that makes the difference. Functions are fundamental to JavaScript, and many of the techniques we use to design and organize code depend on advanced knowledge and use of functions. The path to learning functions at this level is an interesting and often mind-bending one, so get ready... This chapter is going to be a bit like Willy Wonka giving a tour of the chocolate factory—you’re going to encounter some wild, wacky and wonderful things as you learn more about JavaScript functions.

Note

We’ll spare you the singing Oompa Loompas.

The mysterious double life of the function keyword

image with no caption

So far we’ve been declaring functions like this:

image with no caption

There are no surprises here, but let’s get our terminology down: formally, the first statement above is a function declaration, which creates a function that has a name—in this case quack—that can be used to reference and invoke the function.

So far so good, but the story gets more mysterious because, as you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required