The order in which JavaScript is executed

Source: Internet
Author: User

1. By default, the browser parses the HTML code from top to bottom, and of course the script is embedded in the HTML, so it is also in this order, and the files that are introduced outside do not change this order.

2. Any JavaScript code will be compiled before execution

3. When parsing to script the browser invokes the JS parser, usually the JS parser is pre-compiled, then parsed, and the parser compiles the variables and function declarations. if it has not been previously declared, the compiler declares a variable at the current scope, initializes it, but the variable is not assigned a value at this time

In this case, the compiler will first declare a variable in the current scope, initialize it, that is, num (if it was not previously declared), But at this time the variable is not assigned , so there will be undefined

Next, the engine looks for the variable in the scope, and if it can find it, it will be assigned a value, so there will be 1.

4. Function compilation

When a function name is the same, the following function replaces the previous function

The 5.js parser is precompiled and parsed according to the script fragment , compiles the first script fragment and executes, then compiles a second script fragment and executes

6.

In different script snippets, variables can be shared (this is for global variables and functions that can be shared). Local variables should be placed in the function.

If there is a problem with this code when executing a script snippet, the code following the current script is stopped, but there is no effect on the subsequent script fragment.

The order in which JavaScript is executed

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.