They’re effectively identical in most modern JS runtimes, for practical purposes. If you assign a function expressions to a variable, as you’re doing there, they implicitly do have a name, which is, as you say, the name of the variable. This is very easy to demonstrate:
const example1 = () => void 0;
const example2 = function () {
return void 0;
};
function example3 () {
return void 0;
}
console.log(example1.name); // logs example1
console.log(example2.name); // logs example2
console.log(example3.name); // logs example3
There are differences, but they’re very slight: you can freely use functions as values in Javascript, normally passing them to other functions, for example:
[1,2,3].forEach((v) => console.log("Value: ", v))
That’s using an anonymous function (without any name there). But equally, could do
function logValue (v) {
console.log("Value: ", v);
}
[1,2,3].forEach(logValue);
Using a function declaration. And it does exactly the same thing.
Few things that are different:
- anonymous functions can be used to create immediately invoked function expressions, ie a function that is immediately executed, which is useful in a number of contexts.
- function declarations are hoisted to the top of the scope (this means you can call them [in the code] before they are defined [later on in the code]). Function expressions are not, they go in order, and you’ll get a syntax error if you try to access them before they’re defined (they’re attached to a variable, so it’s exactly the same rules as variables).
- arrow functions don’t have
arguments
or the [non-standard] caller
or callee
properties, you’ll get a syntax error if you try to access them.