S
SolidJS•8mo ago
Eatham

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•8mo 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)
Eatham
EathamOP•8mo ago
Thanks
Want results from more Discord servers?
Add your server