Let's consider the following code:
+function func() {
console.log('!');
}
As you already know, this function is a function expression, regardless of the fact that it has a name (we already found out that the presence of a name is not a criterion at all). Remove this plus - and get Function Declaration:
function func() {
console.log('!');
}
Let's put +
on the line above before
the function - it will again become a
Function Expression:
+
function func() {
console.log('!');
}
And now after the plus we put the number
1
and a semicolon - our function
will become a Function Declaration:
+1;
function func() {
console.log('!');
}
Why so: because on the first line one complete command is written, closed with a semicolon. Therefore, this command does not affect our function in any way.
In fact, the semicolon can be removed, because in JavaScript it is not required - the function will still remain a Function Declaration:
+1
function func() {
console.log('!');
}
But if after 1
you put one more plus,
then the function will become a Function
Expression:
+1+
function func() {
console.log('!');
}
Why so: because on the first line there is an unfinished expression - there is a plus and nothing after it. Therefore, the JavaScript interpreter thinks that this plus refers to the line below, that is, to our function.
If there is a complete expression on the first line, then JavaScript automatically puts a semicolon on it and this expression does not affect our function in any way.
Determine if the presented function is a Function Declaration or a Function Expression:
-
function func() {
console.log('!');
}
Determine if the presented function is a Function Declaration or a Function Expression:
-1;
function func() {
console.log('!');
}
Determine if the presented function is a Function Declaration or a Function Expression:
-1
function func() {
console.log('!');
}
Determine if the presented function is a Function Declaration or a Function Expression:
1
function func() {
console.log('!');
}
Determine if the presented function is a Function Declaration or a Function Expression:
-1-
function func() {
console.log('!');
}