Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Responding to User Interaction Functions as Parameters

Peter Ajuzie
Peter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 Points

An alternative: functions as parameters

Please would this code below serve as a viable alternative to the functional expression method for passing functions as parameters?

// const say = something => console.log(something);

const exec = (func, arg) => func(arg);


let say; // Would not work with const so we'll switch to let and pass that function on line 1 directly into the function below as an argument
exec(say = something => console.log(something), 'Hi, there');

1 Answer

Steven Parker
Steven Parker
218,829 Points

I wouldn't call this "alternative" as it is the functional expression method. You just substituted the "arrow function" form for the convention form to create the function expression. And yes, the two forms are interchangeable, as long as you observe the restrictions of arrow functions.

But if you're not going to define "say" in advance, you could just pass the function expression in by itself, as shown in the latter part of the video:

exec(something => console.log(something), 'Hi, there');

And if you had a need to use the function later, it would make for more readable code to define it outside the call.