First of all, I don’t understand function declarations and function expressions very thoroughly. I basically use function declarations, and I know that they will be prefixed. In my mind, I don’t know if the function expression has a function name? Because I haven’t used it much. Today I saw assigning a function to a variable, and found that it seems to be the same as a function expression. I hope someone can answer it, thank you.
var s = function square (x){ return x*x; } console.log(square(4)); console.log(s(4))
The above question 1, the following is question 2, why the above code reports an error "square is not defined", while the following code outputs normally, because the above code belongs to a function expression, so square(4) is not supported Yeah? As I write this, I suddenly want to ask, the function expression is to write on the same line before the function function, and assigning the function to a variable is just based on the function declaration, starting a new line , is that so? Please help, thank you
function square (x){ return x*x; } var s = square; console.log(square(4)); console.log(s(4))
Regarding question 2,
expression is to directly create a function as a variable, and the external function does not exist.
When a function is assigned to a variable, the function comes first and then the variable.
So we have the answer to question one. s itself is a function, and this function does not exist externally, so the error does not exist.
In addition, it should be written as:
Two questions have the same answer. The function name of a function expression is read-only and can only be referenced within this function. It is generally used for recursion.