Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Daniel L.
Daniel L.
10,837 Points

Does arg and something do the same thing in this?

I know a lot of people get confused with this and I'm one of them. I think I'm getting close to understanding but I just can't connect the dots.

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

exec ((something) => {
   console.log(something);
}, "Hi, there");

I don't understand what is happening. I understand that the anonymous function with the something parameter is in the place of the func parameter from the exec function. But what is the point of the arg and the something. Aren't they essentially the same thing here? We are asking that the console logs (something) but what it logs is arg.

1 Answer

To illustrate why you need the something parameter, you should just try and see how this would run if you took away the something paramter.

exec(() => console.log(), "hi there")

Nothing will happen. exec will always pass an argument arg to func, so the function passed must accept a parameter. If it doesn't, then exec can't actually pass an argument to it. So whatever callback you pass to it, it must accept at least one argument or it won't work as intended. I hope that makes sense. This is a bit tricky and I even had to think about it for a minute.

A word of consolation though: I've gone all through the techdegree and worked on a lot of independent projects, and never once have I had a use case for writing a function like this. This is more of a brain teaser that is good to wrestle with, but I don't think it has many practical applications.

Daniel L.
Daniel L.
10,837 Points

Thanks Michael! That is very encouraging and congratulations on finishing the Techdegree that's awesome!

I've been struggling with this on and off for a few days, I'll think I get it then I get confused again. I just finished the Callback functions course which helped a lot, I honestly think it should be included in the Full Stack JavaScript Track I'm doing.

Your answer made a lot of sense, I think I have it now, maybe lol, it's like I have a grasp just not a firm grasp lol. It really is a brain teaser. But I'm getting there.

Thanks again!

Daniel L. I know that feeling, like you get it, but just barely, and the slightest breeze will blow away your understanding. Keep working through courses, taking notes, and building projects. You'll come back across something like this eventually in the future, and you'll think to yourself "I can't believe I found that so hard to grasp back a few months ago". And like I said, functions that accept a callback and a parameter that gets passed to the callback aren't common. As long as you understand the basics of callbacks, you're ready to learn asynchronous programming. Asynchronous programming is challenging, but it will open up all kinds of exciting new JavaScript adventures for you, from working with Node.js and the Express framework to using fetch to interact with APIs, to using Object Relational Mappers to interact with databases. Have fun!