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
1. Download the virtual machine Official download...
Based on daily development experience and relevan...
MySQL paging analysis principle and efficiency im...
Several problems were discovered during the use o...
<html> <head> <meta http-equiv=&quo...
<br />Previous article: Seven Principles of ...
Table of contents 1. Introduction 2. Initial Vue ...
Table of contents Step 1: Install node_modules in...
I was in a meeting when a colleague called to rep...
Today at work, a friend I added temporarily asked ...
What is an index? Why create an index? Indexes ar...
Preface In order to ensure the consistency and in...
Table of contents Preface Input box component lay...
This article example shares the specific code of ...
CSS attribute selectors are amazing. They can hel...