当前位置:网站首页>let关键字的特点
let关键字的特点
2022-07-20 21:20:00 【InfoQ】
前言
let
特点
- let声明的变量只在所处于的块级作用域有效
- 不存在变量提升
- 有暂时性死区
块级作用域🧐
if (true) {
let a = 10;
console.log(a); // 10
}
复制代码
if (true) {
let a = 10;
}
console.log(a);
复制代码
a is not defined
let
var
好处🪐
for (var i = 0; i < 3; i++) {
}
console.log(i);
复制代码
for (let i = 0; i < 3; i++) {
}
console.log(i); // i is not defined
复制代码
不存在变量提升
let
a = 10;
console.log(a); // 10
var a;
复制代码
a = 10;
console.log(a); // 报错
let a;
复制代码
暂时性死区
var num = 10;
if (true) {
console.log(num); // 10
}
复制代码
问题来了
var num = 10;
if (true) {
console.log(num);
let num = 20;
}
// Cannot access 'num' before initialization
复制代码
边栏推荐
猜你喜欢
CAN物理层介绍
使用优先队列priority_queue实现对基unordered_map于value值的数值筛选
2021年山东省职业院校技能大赛中职组”网络安全“正式赛题
【7月考试战报】ORACLE OCP 19C考试通过
asp. Net - edit hyperlink event
伦敦银实时行情能够给人们带来哪方面的信息
Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic
福昕软件亮相2022年全国化工企业数智化转型发展论坛
微信小程序--》数据绑定和事件绑定
Section 2 of Chapter 2: string index and slicing
随机推荐
spinlock 使用介绍
BGP (border gateway protocol) border gateway protocol
Section 14 of Chapter 2: the concept of dictionary
推荐几个接私活的利器
Lambda表达式
C#单例模式
【Coggle 30 Days of ML】糖尿病遗传风险检测挑战赛(1)
04 extract the latest data
Devaxpress xtrareport report development related knowledge points
Lambda expression
买了一台云服务器到底能干什么?到底有哪些新玩法?
Process the cached image of the browser, and only request it once
Is CICC securities a regular securities company? Is it safe to open a stock account?
8 月亚马逊云科技培训与认证课程,精彩不容错过!
第二章第十五节:字典的基本操作
经典赛题-Web渗透测试记录
Many people think that the development logic of the metauniverse is consistent with that of the Internet, but I don't think so
Origin 2022下载保姆级安装教程
What can I do with buying a cloud server? What are the new ways to play?
A solution to std:: string cannot cross DLLs