当前位置:网站首页>The difference between let and VaR
The difference between let and VaR
2022-04-22 12:27:00 【Yunmo - brother Kuang's blog】
Here is an explanation of a previous blog
Blog address :let and var The difference between , Several small questions
The first question is :
console.log(dog); //undefined
var dog = " puppy "
console.log(cat); //Error: Cannot access 'cat' before initialization
let cat = " kitten "
explain :
The first output , Because var Declared variables will result in variable Promotion , In short, it is the process of pre parsing variables , Will be assigned to undefined.
The second report is wrong , Because let The declared variable does not have a variable promotion
The second question is :
The first 1 Group
var dad = ' I'm daddy !'
console.log(dad); // I'm daddy !
var dad = ' I'm the father !'
console.log(dad); // I'm the father !
The first 2 Group
let son = ' I'm a son !'
console.log(son); // I'm a son !
let son = ' I'm the son !'
console.log(son); //Error: 'son' has already been declared
explain :
The first group can output normally , Because var Declared variables can be declared repeatedly
The second report is wrong , Because let Declared variables cannot be declared repeatedly
Third question
{
let a = 1;
{
// Block level scope
let b = 2;
console.log(a); // 1
}
console.log(b); // b is not defined
}
explain :
The first output , Because the current block level scope does not have a This variable , It will automatically look up the scope one level up , In the parent scope a It's assigned a value 1
The second report is wrong , Because let Declared variables automatically generate block level scopes ,b Is a variable declared at the block level scope , The outer scope cannot be accessed
Fourth question
for (let i = 0; i < 3; i++) {
console.log(i); // 1 2
}
console.log(i); //Error: i is not defined
explain :
The first output , Because for In circulation let Declared variables automatically generate block level scopes ,i Is a variable declared at the block level scope , Within this block level scope, you can normally access
The second report is wrong , Because it's beyond i Block level scope
Fifth question
The first 1 Group
var monkey = ' I'm the monkey king !';
{
console.log(monkey); // I'm the monkey king !
var monkey = ' I'm a six eared macaque ';
}
console.log(monkey); // I'm a six eared macaque
The first 2 Group
let monkey = ' I'm the monkey king !';
{
console.log(monkey); // Error: Cannot access 'monkey' before initialization
let monkey = ' I'm a six eared macaque ';
}
console.log(monkey); // I'm the monkey king !
explain :
The first group can output normally , Because var The declared variable does not have a block level scope
The second report is wrong , Because ES6 Specifies when we use... In a block level scope let and const To declare variables , This variable will initially form a closed scope , In other words, even if the variable with the same name exists in the upward scope, we cannot access
Here, we should pay attention to distinguish between functions var And let The difference between , The code is as follows
<script> let a = 1; (function () {
console.log(a); // There will be an error let a = 2; })(); </script>`
版权声明
本文为[Yunmo - brother Kuang's blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221225117941.html
边栏推荐
- Comparison of data protection modes between Oracle data guard and Jincang kingbasees cluster
- [in depth understanding of tcallusdb technology] example code - asynchronous call interface
- What is the difference between CPU and GPU?
- Case 4-1.7: file transfer (concurrent search)
- Codeforces Round #783 (Div. 2)
- Oracle Data Guard和金仓KingbaseES集群的数据保护模式对比
- nt10. 0 system (server2016 / 2019) runtimebroker abnormally shut down, associated event ID 142 / 143 / 226 / 227 / 228, etc
- 模糊集合论
- Base64 encryption, decryption and JSON processing
- 4.21学习记录 DP记录路径(LCS)树上背包(选课)一般树形DP(没有上司的舞会)
猜你喜欢

LeetCode 695、岛屿的最大面积

ROS2学习笔记(七)从turtlesim学习ROS2的工具
![[concurrent programming 051] implementation principle of volatile memory semantics](/img/48/0a42f47d44d1a9385d723252629170.png)
[concurrent programming 051] implementation principle of volatile memory semantics

JS基础8

分享一下自己最近写的一个移动端项目积累的一些实用技巧
![[concurrent programming 055] will the following daemon thread execute the code in the finally module?](/img/e8/92d33e701d3b1691e30885d4111315.png)
[concurrent programming 055] will the following daemon thread execute the code in the finally module?

【并发编程050】内存屏障的种类以及说明?
![【深入理解TcaplusDB技术】删除列表所有数据接口说明——[List表]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
【深入理解TcaplusDB技术】删除列表所有数据接口说明——[List表]
![[concurrent programming 053] Why use volatile variables for double check lock variables](/img/d9/0a23a33c6573edaaa63ae588f13af9.png)
[concurrent programming 053] Why use volatile variables for double check lock variables

ROS2学习笔记(九)从turtlesim学习ROS2的bag录制
随机推荐
Best buy website EDI test process
let和var的区别面试题答案
TypeError: connection.connect is not a function
【并发编程052】说说双重检查锁以及其优点?
What is the difference between CPU and GPU?
JS基础14
Application case sharing of isolated integrated current sensor ch704 which can measure current above 50A
LeetCode 34、在排序数组中查找元素的第一个和最后一个位置
Free trial for the first three months! Borui data alarm platform onealert is in progress
What role does RF chip play in mobile phone?
Take you to Huawei cloud Conference [play with Huawei cloud]
H5<canvas>标签+原生JS 自制一个简易画图工具
Difference between redis setex and set
Comparison of data protection modes between Oracle data guard and Jincang kingbasees cluster
【深入理解TcaplusDB技术】将数据插入到列表指定位置接口说明——[List表]
[in depth understanding of tcallusdb technology] example code - asynchronous call interface
Distributed transaction and lock
JS盒子点击时跟随鼠标移动
C语言%7.2d、%-7d、%7.2f、%0.2f的含义
[concurrent programming 047] cache locking performance is better than bus locking. Why not eliminate bus locking?