Preface: 1. Scopes are expressed in different formsvar is function scope, let is block scope { var monkey = 'Xun Wukong'; let pig='Pork chop cover'; } console.log(monkey); //output undefined console.log(pig); //Error: pig is not deined As can be seen from the above code, variables declared with 2. The difference between variable promotion and non-promotionVariables declared with var will be hoisted, but variables declared with let will not be hoisted. console.log(monkey); //undefined var monkey = 'Xun Wukong'; console.log(pig); //Error: pig is not defined let pig='Pork chop cover'; Following the same logic, why a variable declared with var is shown as undefined when called before it is declared, while a variable declared with let throws an exception when called before it is declared? This is the difference between the two in variable promotion. Variables declared with var have variable promotion, while variables declared with let do not. So what is variable promotion? I will not give a conceptual description here. I will only say my personal understanding that the above code is actually equivalent to the following: var monkey; console.log(monkey); //undefined monkey = 'Xun Wukong'; console.log(pig); //Error: pig is not defined let pig='Pork chop cover'; Do you see the difference? The variable declared with var will be extracted to the top of the scope for definition but no value will be assigned. The assignment operation is still in your code, so when you call the variable declared with 3. Differences in Temporary Dead ZonesTemporary dead zone: If a variable is let in a scope, and there is a variable with the same name in the outer scope, then even if the variable is changed in the scope, it will not affect the outer scope. The specific performance is as follows: for(var i=0;i<5;i++){ setTimeout(function(){ console.log(i) },1000) } for(let i=0;i<5;i++){ setTimeout(function(){ console.log(i) },1000) } What are the results of running these two codes? The result of the first code is that 5 5s are printed in sequence after 1 second. The result of the second code is that 0, 1, 2, 3, 4 are printed in sequence after 1 second. Why does this difference exist? Because the variable i in the first code is declared by the var keyword, there is no critical dead zone, that is, the variable i you access in The variable i in the second code is declared by the let keyword, which creates a critical dead zone. The i variable in 4. In the same context, var can be repeatedly declared, but let cannotlet monkey='Xun Wukong'; let monkey = '逼马吻'; //Error: Identifier 'a' has already been declared var pig = 'Pork chop cover'; var pig = 'pig anal fissure'; //Normal access, the value of the variable pig is replaced This is the end of this article about the difference between var and let in JavaScript. For more information about var and let in JavaScript, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to find and delete duplicate rows in MySQL
>>: Example code for drawing double arrows in CSS common styles
Preface I have always wanted to know how a SQL st...
Table of contents animate() animation method Anim...
In SQL, GROUP BY is used to group data in the res...
Forwarding between two different servers Enable p...
1. In addition to the default port 8080, we try t...
New features in MySQL 8.0 include: Full out-of-th...
Here, I have mainly sorted out some commonly used...
This article example shares the specific code of ...
The solution to the problem that mysql cannot be ...
Examples: Through the PHP background code, you ca...
Method 1: MySQL provides a command line parameter...
Mysql limit paging statement usage Compared with ...
This article shares the MySQL Workbench installat...
The Linux seq command can generate lists of numbe...
The fixed layout of the page header was previousl...