0

function say(something) {
  console.log(something);
}

function exec(func, arg) {
  return func(arg);
}

exec(say, "Hi, there.");

Why does this code work? I feel like it shouldn't since what is in the second function should return

say(something) (arg) { 
  console.log(something) ; 
}
7
  • actually the question is unclear. what do you want and what does not work? Commented Apr 11, 2019 at 8:12
  • @NinaScholz i dont understand why the code returns Hi, there. Commented Apr 11, 2019 at 8:19
  • say(something) (arg) { console.log(something) ; } <-- this to me is a problem @NinaScholz Commented Apr 11, 2019 at 8:20
  • "i dont understand why the code returns Hi, there" ...it doesn't return that value, it logs it to the console inside the say() function. If you logged the output of exec(say, "Hi, there."); it would be undefined. Commented Apr 11, 2019 at 8:21
  • the second one is no valid javascript. Commented Apr 11, 2019 at 8:21

3 Answers 3

1

It works as it does because when you write return func(arg);, the func(arg) bit executes the function. What is returned is then the result of executing func(arg).

However, you're right that say() doesn't actually return a value, it just logs to the console within that function. The output you're seeing is the result of that log command. If you logged the returned value instead you'd see undefined as the result.

But if you'd passed a different function to exec which did return a value, then you'd need the return in exec() for it to work properly.

P.S. I'm not sure if this is part of what you're asking, but the difference between that and when you wrote exec(say, "hi there"); is that in that code, say is a reference to the "say" function. At that moment it's treated like any other variable, just the same as if you passed in a number or a string, for example.

The difference is the (), which causes the function to be executed, rather than passed as a reference.

In the question you seem to imply that you'd expect the source code of the say() function to be displayed, but this is not what it does, and also is not possible anyway.

Sign up to request clarification or add additional context in comments.

5 Comments

so the second function returns say(("Hi, there.")) {console.log("Hi,there.") ;} ?
Sort of. It returns the result of executing say("Hi, there."). In this case, that result happens to be undefined, because the say() function does not return any value.
Imagine instead of return func(arg); you had written var result = func(arg); return result;. Does that make it clearer?
ok i see but dont you need the next two characters => to pass an anonymous function
You mean this syntax: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… ? no, you don't need that, that's a different way of defining a non-reusable function (and it has, in certain circumstances, different behaviours which you should be careful about).
1

Function say returns undefined (default value if no return keyword is used), and therefore function exec also returns undefined.

Proof:

function say(something) {
  console.log(something);
}

function exec(func, arg) {
  var result = func(arg);
  console.log(result);
  return result;
}

var result2 = exec(say, "Hi, there.");
console.log(result2);

1 Comment

thank you for the help i will upvote your post as soon as i can
1

Maybe you are looking for a closure, where exec returns a function for getting arg for the first handed over function func.

function say(something) {
    console.log(something);
}

function exec(func) {
    return function (arg) {
        return func(arg);
    };
}

exec(say)("Hi, there.");

3 Comments

i dont know what a closure is but i will learn about them and upvote your post as soon as i can. Thank you for the help.
@6Michael6Myers6 Basically in this case the closure is a neater (and maybe cleverer) way of writing function say(something) { console.log(something); } function exec(func) { var f = function (arg) { return func(arg); }; return f; } var newFunc = exec(say); newFunc("Hi, there");. That might help you to understand what is happening here. In your simple example it's not massively useful, but would have more value if exec() was doing more work. See medium.freecodecamp.org/… for examples.
@ADyson just finished reading about closures on mdn now im a read the article you gave me a link to thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.