How to execute an array of functions in JavaScript?
Tags: javascript
Published: 2021-06-25 00:00:00

---
  title: How to execute an array of functions in JavaScript?
  published: true
  tags: JavaScript
  date: Fri Jun 25 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/execute-or-call-array-of-functions-javascript
  cover_image: https://melvingeorge.me/_next/static/images/main-5848ef1d7dc561a4344b61d89b7a56b6.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/execute-or-call-array-of-functions-javascript)
  
To execute an array of functions, we can use a `for` loop or the `forEach()` array method in JavaScript.

### TL;DR

```js
// Functions to execute
function greetPeople() {
  console.log("Hello People!");
}

function sayFirstname() {
  console.log("My First name is Roy");
}

function sayLastname() {
  console.log("My First name is Daniel");
}

// save the functions
// references in an array
const funcsArr = [greetPeople, sayFirstname, sayLastname];

// Loop through each array elements (functions references)
// and invoke the functions
for (let i = 0; i < funcsArr.length; i++) {
  // get the current function getting looped
  const func = funcsArr[i];

  // call the function
  func();
}
```

For example let's say we have some functions like `greetPeople()`, `sayFirstname()` and `sayLastname()` like this,

```js
// Functions to execute
function greetPeople() {
  console.log("Hello People!");
}

function sayFirstname() {
  console.log("My First name is Roy");
}

function sayLastname() {
  console.log("My First name is Daniel");
}
```

Now to execute this funtions in a loop, first let's save all the function references to an `array` like this,

```js
// Functions to execute
function greetPeople() {
  console.log("Hello People!");
}

function sayFirstname() {
  console.log("My First name is Roy");
}

function sayLastname() {
  console.log("My First name is Daniel");
}

// save the functions
// references in an array
const funcArr = [greetPeople, sayFirstname, sayLastname];
```

### NOTE: We are only saving the references to the functions in the array and not calling the functions inside the array.

Now to call these functions, let's use a `for` loop and loop through each element in the array (in our case it is the functions references) and call the functions inside the loop. It can be done like this,

```js
// Functions to execute
function greetPeople() {
  console.log("Hello People!");
}

function sayFirstname() {
  console.log("My First name is Roy");
}

function sayLastname() {
  console.log("My First name is Daniel");
}

// save the functions
// references in an array
const funcsArr = [greetPeople, sayFirstname, sayLastname];

// Loop through each array elements (functions references)
// and invoke the functions
for (let i = 0; i < funcsArr.length; i++) {
  // get the current function getting looped
  const func = funcsArr[i];

  // call the function
  func();
}
```

Now if we look into the console we will see this output from the three functions in the array.

```bash
Hello People!
My First name is Roy
My First name is Daniel
```

See the above code live in [JSBin](https://jsbin.com/kiyikehuru/2/edit?js,console)

We can also acheive the same using the `forEach()` array method like this,

```js
// Functions to execute
function greetPeople() {
  console.log("Hello People!");
}

function sayFirstname() {
  console.log("My First name is Roy");
}

function sayLastname() {
  console.log("My First name is Daniel");
}

// save the functions
// references in an array
const funcsArr = [greetPeople, sayFirstname, sayLastname];

// Loop through each array elements (funtions references)
// and invoke the funtions
funcsArr.forEach((func) => {
  // call the current
  // funtion getting looped
  func();
});
```

See the above code live in [JSBin](https://jsbin.com/javufixaki/2/edit?js,console)

The output of using both methods is the same.

That's all 😃!

### Feel free to share if you found this useful 😃.

---