When the parser loads data into the execution environment, it does not discriminate between function declarations and function expressions. The parser is the first to read the function declaration and make it available (accessible) before executing any code. As for a function expression, you must wait until the parser executes to the line of code where it is located before it is actually interpreted. Example:
Alert (sum (10,10)); function sum (NUM1, num2) { return num1 + num2;}
The above code is fully functional. Because the parser has read and added the function declaration to the execution environment before the code begins execution, through a procedure called function declaration hoisting . When code is evaluated, the JavaScript engine declares the function the first time and places them at the top of the source tree. So, even if the code that declares the function is behind the code that calls it, the JavaScript engine can lift the function declaration to the top.
If, as shown in the following example, the function declaration above is changed to an equivalent function expression, it will cause an error during execution.
Alert (sum (10,10)); var function (NUM1, num2) { return num1 + num2;}
The code above generates an error during run time because the function is in an initialization statement, not a function declaration. In other words, a reference to a function is not saved in the variable sum until the statement where the function is executed, and because the first line of code causes "unexpected identifier" (unexpected identifier) error, it does not actually go to the next line.
In addition to the difference between when and how a variable accesses a function, the syntax of a function declaration and a function expression is actually equivalent.
JavaScript function declaration and Function Expression Difference function declaration promotion (Function declaration hoisting)