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 Callback Functions in JavaScript Introduction to Callback Functions Anonymous Function as Arrow Functions

Hayden Lau
Hayden Lau
4,186 Points

Why function executeCallback(callback) { callback(); }; did not also get converted into arrow function?

Can someone explain to me in the arrow function tutorial,

They only converted the Print Hello and Print Goodbye into arrow function. but not the function declaration of executeCallBack(callback)?

Below is the code:

function executeCallback(callback) { callback(); };

//Print Hello executeCallback(() => console.log('Hello'));

//Print Goodbye

executeCallback(() => console.log('Goodbye'));



1 Answer

Steven Parker
Steven Parker
216,164 Points

The other functions are anonymous, and the conversion of anonymous functions seems to be the point of this lesson.

But the named function could also be written in the "arrow" style:

const executeCallback = callback => callback();

But be aware that arrow functions can't always replace conventional ones, particularly as constructors, class methods, generators, and anywhere "this" or "arguments" are used. For more details, see the MDN page on Arrow Functions.

Hayden Lau
Hayden Lau
4,186 Points

Got it. Thanks for the help!