1. Function declaration
1 // function Declaration 2 function fundeclaration (type) {3 return type=== "Declaration"; 4 }
2. Function expression
// function Expression var function (type) { return type=== "Expression"; }
The JS code will perform a function promotion at parse time, so the function can be called within the same scope regardless of where the function declaration is defined. The value of the function expression is determined at the JS runtime and is called after the expression assignment is complete.
This tiny difference may lead to unexpected bugs in the JS code, which can cause you to fall into an inexplicable trap. The following code:
1 fundeclaration ("Declaration"); // => true 2 function Fundeclaration (type) { 3 return type=== "Declaration" 4 }
1 Funexpression ("Expression"); // =>error 2 var function (type) {3 return type=== "Expression"; 4 }
The above error occurs because functions created with a function declaration can be called after the function is parsed (parsing is done with logical processing);
A function created with a function expression is assigned at run time and is not invoked until the expression has been assigned the completion value. The essential reasons are the differences between these two types in JavaScript function hoisting (function promotion) and runtime timing (parsing/runtime).
The relationship between the JS function declaration and the function expression