There are two ways to declare functions in JavaScript: function declaration and function expression.
The differences are as follows:
1). For functions defined by function declaration, the function name is required, while the function name of the function expression is optional.
2). For functions defined by function declaration, the function can be called before the function declaration, while the function of the function expression can only be called after the declaration.
3). Functions defined by function declaration are not real declarations. They can only appear globally or nested in other functions, but they cannot appear in loops, conditions or try/catch/ finally in, and
Function expressions can be declared anywhere.
The functions are defined in two ways below:
An interesting javascript below:
What will be output? The first reaction should be "I am outside". The result is "I am inside" in chrome, IE11 directly reports an error, and the lower version of firefox outputs "I am outside"...
The results output by chrome clearly reflect the characteristics of functions declared using function declarations-the function can be called before it is declared.
IE error shows that the object is missing because the function is declared in the condition, which violates the principle of function declaration.
Scope of function expression:
If the function declared by the function expression has a function name, then the function name is equivalent to a local variable of the function and can only be called inside the function. For example: