Function as an Argument

Overlook on functions being passed as arguments to other functions.

Background

There are occasions when you need functions inside functions. This may sound like a nested function problem, but sometimes one function is dependent on another function’s input to trigger a particular output. For this, we pass functions as arguments.

Introduction

Functions can be passed into other functions by being passed as arguments. This is because functions in JavaScript are objects. The functions that take other functions as an argument or return functions are called higher-order functions. Meanwhile, functions passed as arguments are called callback functions. Let’s look at some examples.

Passing a regular function as an argument

In the following example, we pass a regular function as an argument.

Get hands-on with 1400+ tech skills courses.