The function declaration in JS exists "use first, then define"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.