I get the value of arrow functions as shorthand in lots of contexts, but the course seems to use them almost exclusively with variables, instead of just building separate functions. Why would I make a variable and assign an arrow function when I could just create a new function? Is the functionality different somehow?
const newArrow = () => {};
function newNormal() {}
This is complex topic that would be beyond the scope of a regular reply. I would have to branch out into defining argument objects and more underlying theory.
Here’s an article for a start:
There are different use cases for both, like always in coding depending on your intends and the context.
Good link. Thanks. I’m not understanding the difference, other than syntax between variable = ()=> {}
variable = function() {}
The #4 in the article explained this and said ‘arrow function can’t be declared’ but I don’t see a difference between the two. Are they just saying you can’t write
function name () => {}
but it’s ok to have
function name() {}
seems no different.
Maybe I get it and it just seems like nothing to me, but maybe I’m missing something.
You are trying to combine a named with an anonymous function, which is on of the basic differences between regular (requires a name and the function keyword to declare it) and an arrow function (is always anonymous, needs to be assigned to a variable).
Use cases: Named functions for functions that can be reused and have a clear purpose, good for debugging since the function name appears in the console.
Anonymous functions like arrow functions for short-lived, in-block purposes. For example forEach() and map() methods are using arrow functions. Their main advantage is less code.
Often it is just a matter of preference. Without having seen all of the curriculum, I think their use is mainly just preference.
But there are a few differences you should know about.
The main one is how this works with the functions.
Accidental redeclaration is protected when using const/let and hoisting doesn’t work (1 is usually always a benefit. 2 is more of a preference, are you ok with the function being called before their declaration).