O'Reilly logo

Coding All-in-One For Dummies by Nikhil Abraham

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 3

Understanding Callbacks and Closures

IN THIS CHAPTER

check Understanding callback functions

check Using callbacks

check Creating closures

O, call back yesterday, bid time return.”

— EARL OF SALISBURY, RICHARD II

Callbacks and closures are two of the most useful and widely used techniques in JavaScript. In this chapter, you find out how and why to pass functions as parameters to other functions.

What Are Callbacks?

remember JavaScript functions are objects. This statement is the key to understanding many of the more advanced JavaScript topics, including callback functions.

Functions, like any other object, can be assigned to variables, be passed as arguments to other functions, and created within and returned from functions.

Passing functions as arguments

A callback function is a function that is passed as an argument to another function. Callback functions are a technique that’s possible in JavaScript because of the fact that functions are objects.

Function objects contain a string with the code of the function. When you call a function by naming the function, followed by ( ), you’re telling ...

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