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 trialShay Paustovsky
969 PointsCallback functions and nested functions clarification
Hello,
So I am a bit confused with the idea of passing a function as an argument to another function.
I have this code example and would love if someone could clarify it a bit for me :
function exec(func, arg) {
func(arg);
}
exec((something) => {
console.log(something);},
"Hi, There");
My Question is :
Why does the function output the string "Hi, There" , but the anonymous function requires a parameter named "something" how does it work together?.
Thanks a lot in advance.
Shay
3 Answers
Steven Parker
231,268 PointsThis example is a bit of a mind-bender. But the basic idea is that "Hi, There" gets substituted for "something" when the anonymous function is called.
Since all the anonymous function does is to call "console.log", you could simplify the call to this, which might be a bit less confusing:
exec(console.log, "Hi, There");
Humoyunmirzo Nosirov
Courses Plus Student 5,154 Pointsyep that is a bit confusing ,
Shay Paustovsky
969 PointsYou're the best Steven,
thanks alot
Shay