0

If I call the following function

console.log("Hello World!");

I expect it to print Hello World! and thats all fine and dandy, but what happens when I add a local function, like

console.log(target, function(err, reply) {
        console.log("Hello World!")
    });

It seems I can add on this local function to anything

foo(function(err, reply) {
}

I am curious

  1. How the local function operates inside of the other function
  2. Is there a certain way the local function should be handled
  3. Cases where this should be or shouldn't be used.
Caleb Renfroe
  • 183
  • 1
  • 13
  • Please point me to any other question that may answer my confusion, I was unsure as how to phrase the question or relevant terminology so couldn't find anything that answered this for me – Caleb Renfroe Jun 05 '23 at 19:25
  • A function that is passed to another function as an argument is typically called a "callback". – InSync Jun 05 '23 at 19:28
  • Does this answer your question? [Callback function example](https://stackoverflow.com/questions/22442321/callback-function-example) – InSync Jun 05 '23 at 19:28
  • 5
    `console.log()` doesn't call callbacks. So there's nothing special going on there, it's just printing the function itself. – Barmar Jun 05 '23 at 19:30
  • You seem to be asking for general rules of some kind, but broadly across all code that could ever possibly be written in JavaScript. Narrow your scope. Specifically with regards to the code shown, what's not clear to me is (1) why you're passing a function to `console.log` in the first place, (2) what you're expecting the result of that operation to be, or (3) how the observed result of that operation differs from what you expect. With regards to the `foo` function, we don't know what that function does (if anything) with what's passed to it. – David Jun 05 '23 at 19:41
  • Stack Overflow works much better if you have a problem, it's not really a "How does this work sort of site". – Keith Jun 05 '23 at 20:03

2 Answers2

0

Functions, in JavaScript, are first class objects. Anything you can do with an object, you can do with a function, including passing it as an argument.

How the local function operates inside of the other function

The same way it "operates" anywhere else for whatever value of "operate" you care to define. e.g. If you pass it to console.log then it will get logged, it won't be called because the internal logic of console.log doesn't call functions that are passed as arguments to it. (Unlike, for example Array.prototype.sort().)

Is there a certain way the local function should be handled

Not in the general case.

Cases where this should be or shouldn't be used.

That's too broad and too much of a matter of opinion to be answered.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

console.log can accept any number of parameters and will just output all of them to the console.

console.log("hello world!", 69420, true, false, function () {console.log("hello world!");})

Passing a function to console.log will just output the function itself into the console. Also, in the snippet you showed, target is undefined and running it will throw an error.

lucassilvas1
  • 54
  • 1
  • 8