> Other than that, functions should be defined by the keyword.
Says who?
> Other than that, functions should be defined by the keyword.
Says who?
const foo = () => {}
This function is not anonymous, it's called foo.function foo(){} is also callable if bar is defined before foo.
const foo = function (){}
without its own name before (). These behave like expressions and cannot be hoisted.I haven't figured out if people consider this a best practice, but I love doing it. To me the list of called functions is a high-level explanation of the code, and listing all the definitions first just buries the high-level logic "below the fold". Immediately diving into function contents outside of their broader context is confusing to me.
Hoisting also enables cross-imports without helper unit extraction headaches. Many hate js/ts at the “kids hate == and null” level but in reality these languages have a very practical design that wins so many rounds irl.
const foo = () => {}
console.log( foo.name );
actually outputs 'foo', and not the empty string that I was expecting. const test = () => ( () => {} );
const foo = test();
console.log( foo.name );
outputs the empty string.Is this behavior required by the standard ?
var foo = function() {};
Except nowadays it too does have the name "foo".Serious arguments would be:
- readability
- greppability