In this post we will take a look on Higher-Order function in JavaScript. Higher-Order function can be defined as following,

  1. It take one or more functions as parameter
  2. It returns new function as parameter

image

Let us say you have a function as following


function FindGreater(a, b) {
 if (a > b)
 return a;
 else
 return b;
 }

You are going to pass this function as argument to other function. That function will be called as Higher-Order function.

Assume we have created another function with name IamHigherOrderFunction .


function IamHighorderFunction(functionargument) {

return function () {

var result = functionargument.apply(this, arguments);
 return "I am returned from high function" + result;
 }
 }

Some important points about above function are as follows,

  1. It takes a function as argument
  2. It returns a function as output.

In function we are just appending a text to result of passed function as argument. You can call higher-order function as following


var result = IamHighorderFunction(FindGreater);
alert(result(9,5));

You will get output as following. We are passing FindGreater function as argument to Higher-order function named IamHigherorderFunction.

image

You will find use of Higher-Order functions very often in JavaScript. I hope you find this post useful. Thanks for reading.

Advertisements

5 thoughts on “Higher-Order function in JavaScript

  1. Hey –

    Your last post [HIGHER-ORDER FUNCTION IN JAVASCRIPT] was freaking awesome. I have gone ahead and added your stuff to my Feedly account. Please keep me updated if you post anywhere else.

    Keep rocking –

    Jon

  2. Aah, please. Don’t provide incomplete and stale information. Do you know why is it called “higher order” or for that matter of fact, what does “order” mean in the term “Higher order functions”? Do you know that there is something called “Lambda Calculus” in mathematics and “Higher order functions” are related to it? Please stop pretending to be a geek and get your fundamentals right. Your awards dont impress me.
    And by the way, your “IamHighorderFunction” example doesnt make any sense. If it is doing the same computation which can be done by “FindGreater”, what is the point in creating it? Dont just say this is for beginners. Beginners should be taught correctly. You know what, the problem is you lack basics and depth. Get them right first.

  3. Very first sorry if this post was not useful to you. Second as best of my knowledge I have provided correct information here. I can debate with you on same anywhere . If I loose I will stop blogging. And buddy my math may not be as good as yours but concept of JavaScript may be. and no one asked you to get impressed by my award.

    Please do a constructive comment and let me know whats wrong information in above post . Have fun dear developer from Infsoys 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s