Difference between using `const` and `function` to declare a function
I am relatively new to JavaScript and in my time learning I have noticed that people decalare their functions/components using
const
and also function
.
E.g. Using function
And using const:
What are the differences between the two and why would I use one over the other? Thanks 🙂2 Replies
- scoping: arrow functions are block-scoped while functions are function-scoped
- binding: arrow functions cannot have a
this
.
- declaration: functions can be hoisted, arrow functions cannot.
Other than that, it's purely dev preference. I prefer function declaration for top-level functions, arrow functions when passing them (like callbacks)Thanks