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

  • Home
  • SEARCH
  • 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 782155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:20:33+00:00 2026-05-14T20:20:33+00:00

I’m learning lots of javascript these days, and one of the things I’m not

  • 0

I’m learning lots of javascript these days, and one of the things I’m not quite understanding is passing functions as parameters to other functions. I get the concept of doing such things, but I myself can’t come up with any situations where this would be ideal.

My question is:

When do you want to have your javascript functions take another function as a parameter? Why not just assign a variable to that function’s return value and pass that variable to the function like so:

// Why not do this
var foo = doStuff(params);
callerFunction(foo);

//instead of this
callerFunction(doStuff);

I’m confused as to why I would ever choose to do things as in my second example.

Why would you do this? What are some use cases?

Thanks!!

  • 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-05-14T20:20:33+00:00Added an answer on May 14, 2026 at 8:20 pm

    There are several use cases for this:

    1. “Wrapper” functions.

    Lets say you have a bunch of different bits of code. Before and after every bit of code, you want to do something else (eg: log, or try/catch exceptions).

    You can write a “Wrapper” function to handle this. EG:

    function putYourHeadInTheSand(otherFunc) {
        try{
             otherFunc();
        } catch(e) { } // ignore the error
    }
    
    ....
    
    putYourHeadInTheSand(function(){
        // do something here
    });
    putYourHeadInTheSand(function(){
        // do something else
    });
    

    2. Callbacks.

    Lets say you load some data somehow. Rather than locking up the system waiting for it to load, you can load it in the background, and do something with the result when it arrives.

    Now how would you know when it arrives? You could use something like a signal or a mutex, which is hard to write and ugly, or you could just make a callback function. You can pass this callback to the Loader function, which can call it when it’s done.

    Every time you do an XmlHttpRequest, this is pretty much what’s happening. Here’s an example.

    function loadStuff(callback) {
        // Go off and make an XHR or a web worker or somehow generate some data
        var data = ...;
        callback(data);
    }
    
    loadStuff(function(data){
        alert('Now we have the data');
    });
    

    3. Generators/Iterators

    This is similar to callbacks, but instead of only calling the callback once, you might call it multiple times. Imagine your load data function doesn’t just load one bit of data, maybe it loads 200.

    This ends up being very similar to a for/foreach loop, except it’s asynchronous. (You don’t wait for the data, it calls you when it’s ready).

    function forEachData(callback) {
        // generate some data in the background with an XHR or web worker
        callback(data1);
        // generate some more data in the background with an XHR or web worker
        callback(data2);
        //... etc
    }
    
    forEachData(function(data){
        alert('Now we have the data'); // this will happen 2 times with different data each time
    });
    

    4. Lazy loading

    Lets say your function does something with some text. BUT it only needs the text maybe one time out of 5, and the text might be very expensive to load.

    So the code looks like this

    var text = "dsakjlfdsafds"; // imagine we had to calculate lots of expensive things to get this.
    var result = processingFunction(text);
    

    The processing function only actually needs the text 20% of the time! We wasted all that effort loading it those extra times.

    Instead of passing the text, you can pass a function which generates the text, like this:

    var textLoader = function(){ return "dsakjlfdsafds"; }// imagine we had to calculate lots of expensive things to get this.
    var result = processingFunction(textLoader);
    

    You’d have to change your processingFunction to expect another function rather than the text, but that’s really minor. What happens now is that the processingFunction will only call the textLoader the 20% of the time that it needs it. The other 80% of the time, it won’t call the function, and you won’t waste all that effort.

    4a. Caching

    If you’ve got lazy loading happening, then the textLoader function can privately store the result text in a variable once it gets it. The second time someone calls the textLoader, it can just return that variable and avoid the expensive calculation work.

    The code that calls textLoader doesn’t know or care that the data is cached, it’s transparently just faster.

    There are plenty more advanced things you can do by passing around functions, this is just scratching the surface, but hopefully it points you in the right direction 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.