First look at a section of JS code, which uses two ways to declare two functions, respectively, in different places call two functions:
1 <script> 2 ' use strict '; 3// output Hello function 4 console.log (hello); 5 //define Hello function 6 function Hello () {7 alert ("Hello"); 8 } 9 //Output hi variable console.log (HI); //Assign a function to Hi var hi = function () { alert ("HI"); }15 //output variable hi Console.log (HI); </script>
In JS, there are two ways to define a function, namely "function statement" and "expression". -For the Hello function, I called it before I defined it, and I defined how the "function statement" used by the Hello function was defined. -For the HI function, I used the definition of "expression", which was also called before and after the definition. The results are as follows: we can see that functions defined in the way of "function statements" can be "used first, then defined". A function defined with an expression can only be "defined, then used".
The function declaration in JS exists "use first, then define"