JavaScript variable hoisting
In JavaScript, function and variable declarations will be promoted to the top of the function.
In JavaScript, variables can be declared after use, that is, variables can be used first and then declared.
The following two instances will achieve the same results:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> x = 5; // 变量 x 设置为 5 elem = document.getElementById("demo"); // 查找元素 elem.innerHTML = x; // 在元素中显示 x var x; // 声明 x </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var x; // 声明 x x = 5; // 变量 x 设置为 5 elem = document.getElementById("demo"); // 查找元素 elem.innerHTML = x; </script> </body> </html>
Run Instance»
Click" Click the "Run Example" button to view the online example
To understand the above example, you need to understand "hoisting (variable hoisting)".
Variable promotion: Function declarations and variable declarations are always quietly "promoted" to the top of the method body by the interpreter.
JavaScript initialization will not be promoted
JavaScript Only declared variables will be promoted, initialized ones will not.
The results of the following two instances are different:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var x = 5; // 初始化 x var y = 7; // 初始化 y elem = document.getElementById("demo"); // 查找元素 elem.innerHTML = x + " " + y; // 显示 x 和 y </script> </body> </html>
Running instance»
Click the "Run Instance" button to view the online instance
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var x = 5; // 初始化 x elem = document.getElementById("demo"); // 查找元素 elem.innerHTML = "x 为:" + x + ",y 为:" + y; // 显示 x 和 y var y = 7; // 初始化 y </script> </body> </html>
Run Instance»
Click the "Run Example" button to view the online example
The y of instance 2 outputs undefined. This is because the variable declaration (var y) is promoted. But initialization (y = 7) does not promote, so the y variable is an undefined variable.
Example 2 Code similar to the following:
var x = 5; // 初始化 x var y; // 声明 y elem = document.getElementById("demo"); // 查找元素 elem.innerHTML = x + " " + y; // 显示 x 和 y y = 7; // 设置 y 为 7
Declare your variables in the header
JavaScript variable hoisting is unknown to most programmers.
If programmers do not understand variable promotion well, the programs they write will be prone to problems.
In order to avoid these problems, we usually declare these variables before the start of each scope. This is also a normal JavaScript parsing step and is easy for us to understand.
JavaScript strict mode does not allow the use of undeclared variables. In the next chapter we will learn about "strict mode". |