Calling Function With Another Function As A Parameter In JavaScript

Calling Function With Another Function As A Parameter In JavaScript

JavaScript is a powerful language that offers a variety of features to make your code more flexible and reusable. One of these features is the ability to pass functions as parameters to other functions. This functionality is commonly used in callbacks, event handlers, and higher-order functions, and can be extremely useful in a variety of situations. In this blog post, we’ll explore how to call a function with another function as a parameter in JavaScript, with code samples to illustrate each step. We’ll start by defining a basic function and then move on to more complex examples, demonstrating how this technique can be used to write more efficient and effective code.

First, let’s define a function that takes two parameters and returns their sum:

function add(x, y) {
  return x + y;
}

Now, let’s define a second function that takes three parameters: a function, and two values to be passed as parameters to the function. This function will call the function parameter, passing the two values as arguments:

function applyOperation(operation, x, y) {
  return operation(x, y);
}

Notice that the first parameter of the applyOperation function is a function, which we will use as a callback. This is what allows us to pass a function as a parameter to another function.

Now, let’s test our applyOperation function by calling it with our add function as the first parameter:

console.log(applyOperation(add, 2, 3));

This will output 5, since applyOperation will call add(2, 3) and return the result.

We can also define other functions, such as a function that multiplies two numbers:

function multiply(x, y) {
  return x * y;
}

And then call applyOperation with the multiply function:

console.log(applyOperation(multiply, 2, 3));

This will output 6, since applyOperation will call multiply(2, 3) and return the result.

We can also use anonymous functions as parameters to applyOperation, like so:

console.log(applyOperation(function(x, y) {
  return x - y;
}, 2, 3));

This will output -1, since applyOperation will call the anonymous function with parameters 2 and 3, which will return -1.

Finally, we can define a higher-order function that takes a function as a parameter, and returns a new function that applies the given function to all elements of an array:

function mapArray(fn, arr) {
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    result.push(fn(arr[i], 2));
  }
  return result;
}

We can use this function to apply the add function to all elements of an array:

var numbers = [1, 2, 3, 4, 5];
var result = mapArray(add, numbers);
console.log(result);

This will output [3, 4, 5, 6, 7], since mapArray will apply add to each element of the numbers array.

Passing functions as parameters to other functions is a powerful feature of JavaScript that allows for increased flexibility and code reusability. With this technique, you can create higher-order functions, callbacks, and event handlers that make your code more efficient and easier to read.

Share this post

Comments (2)

  • Ali Reply

    result.push(fn(arr[i])); must be result.push(fn(arr[i],2));

    January 14, 2024 at 11:33 AM
    • Esraa Shaheen

      Thank you Ali for reading and following Nile Bits Blog. This is a very good catch that you comment there, I have updated the blog post with your correct line of code.

      January 16, 2024 at 1:21 PM

Leave a Reply

Your email address will not be published. Required fields are marked *