Arrow Functions

For an arrow function, do you need to declare it as a variable? like, const hello = () => {}; or can you just write it as hello = () => {};?

Hello @maxr0006875138, welcome to the forums! Although you do not explicitly require the const, it is better practise to include it.
I hope this helps!

1 Like

If it is going to be a standalone, callable function then it will have to be assigned to a variable so it can be called. An unbound variable is volatile and can be reassigned or even deleted. To protect your functions give them a binding with, const. That will make your function immutable and easier to pass around by reference, if need be.

When writing function expressions as a callback (written in the argument of a function call) they are anonymous and have no assignment. They run immediately in that context.

The latter case will come up in a unit on Higher Order Functions.

1 Like