Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:20:29+00:00 2026-06-09T14:20:29+00:00

While going through Eloquent Javascript (Chapter 6) there is a reference to higher-order functions

  • 0

While going through Eloquent Javascript (Chapter 6) there is a reference to higher-order functions in Javascript. While there is an example provided in Chapter 3, I believe it could be a bit simpler since I still don’t fully understand the concept. After searching the web I can’t seem to find any succinct examples of a higher-order function.

I’d like to see a basic/simple higher-order function in Javascript that will explain the concept.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:20:31+00:00Added an answer on June 9, 2026 at 2:20 pm

    Higher functions are concepts from functional programming. In briefly, a higher function is a function which takes another function as parameter. In javascript, some higher functions are added recently.

    Array.prototype.reduce 
    //With this function, we can do some funny things.
    function sum(array){
        return array.reduce(function(a, b){ return a + b; }, 0);
    }
    

    So, in the above sample, reduce is a higher order function, it takes another function, the anonymous function in the sample, as a parameter. The signature of reduce looks like this

    reduce(func, init);
    //func is a function takes two parameter and returns some value.
    // init is the initial value which would be passed to func
    //when calling reduce, some thing happen
    
    //step 1.
    [1, 2, 3, 4, 5].reduce(function(a, b){ return a + b }, 0);
    //step 2.
    [2, 3, 4, 5].reduce(function(a, b){ return a + b}, 0 + 1);
    //step 3.
    [3, 4, 5].reduce(function(a, b){ return a + b}, 0 + 1 + 2);
    //...
    

    As you can see, reduce iterate an array, and apply the func with init and first element of that array, then bind the result to init.

    Another higher order funciton is filter.

    Array.prototype.filter
    //As the name indicates, it filter out some unwanted values from an Aarry. It also takes a function, which returns a boolean value, true for keeping this element.
    [1, 2, 3, 4, 5].filter(function(ele){ return ele % 2 == 0; });
    

    With the above two examples, I have to say higher order function is not that much easy to understand, especially reduce. But that’s not complex, with higher order function, actually your code would be more clean and readable. Take the filter as example, it tells people that it throws all odd numbers away.

    Here I’d like to implement a simple filter function to show you how.

    function filter(array, func){
        var output = [];
        for(var i = 0; i < array.length; i++){
          if(func(array[i])) output.push(array[i]);
        }
        return output;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While going through many resources on multithreaded programming, reference to volatile specifier usually comes
While I'm going through Python code and seeing functions called, I notice things like
While going through the checkBox I found there is written CheckBox checkbox = (CheckBox)sender
I am new in PHP world. While going through couple of functions i came
I'm getting this error while going through Spring in Action: 3rd edition: Chapter 1.
While going through MSDN I found that in button properties useSubmitBeheviour there is written
While going through Wikipedia's list of sorting algorithms I noticed that there's no stable
While going through SWig generated wrappers, I find that the PInvokes don't have any
While going through some tutorials, I have encountered lines such as this: ((IDisposable)foo).Dispose(); Ignore
While going through Effective C++ (by Scott Meyers), I came across the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.