Usually we see the following two ways to define a function:
// function Statements function fn (str) { console.log (str);}; // definition of an expression var fnx=function(str) { console.log (str+ ' from FNX ');};
Both methods create a new function object, but the function name of the function declaration statement is a variable name, and the variable points to the function object.
As with var declaration variables, functions in function definition statements are displayed to the top of the script or function, so they are visible throughout the script and function, but using the var expression to define the function,
Only the variable declaration is in advance, the variable initialization code is still in place, the function created with the function statement, the function name and the function body are advanced, so we can use it before declaring it.
Console.log (typeof(FN));//functionFN (' abc ');//ABCConsole.log (typeof(FNX));//undefined if(FNX) FNX (' abc ');//Would not execute ElseConsole.log (' Fnx is undefined ');//Fnx is undefined //function Statements functionfn (str) {console.log (str); }; //definition of an expression varfnx=function(str) {console.log (str+ ' from FNX '); };