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
function MyComponent(props) {
return <div></div>;
}
function MyComponent(props) {
return <div></div>;
}
And using const:
const MyComponent = (props) => {
return <div></div>;
}
const MyComponent = (props) => {
return <div></div>;
}
What are the differences between the two and why would I use one over the other? Thanks šŸ™‚
2 Replies
lxsmnsyc
lxsmnsycā€¢4mo ago
- 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)
eatingtheham
eatingthehamā€¢4mo ago
Thanks