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 Basics (Retired) Creating Reusable Code with Functions Introducing Functions

Jake Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
Jake Smith
Data Analysis Techdegree Student 15,921 Points

What are some use cases for function declarations vs function expressions?

I don't understand the advantage of using one vs the other. Some use cases would illustrate this more and improve my understanding. Thanks! :)

1 Answer

Steven Parker
Steven Parker
231,271 Points

When creating a function that will be used frequently throughout the code, a function declaration is a good choice.

But if you're creating a function to be passed to another function as a callback, and won't be used anywhere else, a function expression can be used to create it anonymously directly in the other function call.

But I'm not sure the concept of "callback" has even been covered yet, so I'd suggest that you might continue on with your studies and wait until you get to concepts like that where you'll see plenty of examples of function expressions being used. It should make much more sense then.

Jake Smith
seal-mask
.a{fill-rule:evenodd;}techdegree
Jake Smith
Data Analysis Techdegree Student 15,921 Points

Steven,

That makes a lot of sense. I do roughly understand the concept of a callback function.

Thank you for the clarification!