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 JavaScript and the DOM (Retiring) Responding to User Interaction Functions as Parameters

Peter Ajuzie
seal-mask
.a{fill-rule:evenodd;}techdegree
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);


//  THIS QUESTION REFERS TO THIS CODE BELOW

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
231,268 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.