Let's now make a function that will have a name, but it will be a Function Expression, because it participates in the expression:
+function func() {
console.log('!');
}
It is interesting that by the name func
we will not be able to refer to our function,
this will lead to an error:
+function func() {
console.log('!');
}
func(); //!! throws an error
To be able to call our function, we need to assign it to some variable:
let test = function func() {
console.log('!');
};
test(); // shows '!'
Once again: a function that is a function expression cannot be called by its name. Such a function can only be called using the variable in which this function was written.
But nevertheless, a function expression can have a function name, it will be syntactically correct. Why this is necessary, we will understand in the next lessons.
To summarize: functions are Function Declaration or Function Expression not because they have a name or not, but because they are members of expressions or not.
As you saw above, a function without a name can be treated as a Function Declaration, and a function with a name can be a Function Expression.