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

Passing A Function Into A Function

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 whats in the second function should return

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

2 Answers

Steven Parker
Steven Parker
230,024 Points

It doesn't matter that "say" does not return anything. The "exec" function will just return "undefined".

And the way "exec" is being called, its own return value is not being used anyway.

Hello

This works because you are passing the function without executing it first (your creating a callback function)

If you added parentheses in the parameter then it would no longer work as the function will be invoked immediately.

So your above code is an acceptable callback function