当前位置:网站首页>Differences between let and const, let, const and VaR
Differences between let and const, let, const and VaR
2022-07-19 01:35:00 【Hemubai】
List of articles
2. Duplicate statements are not allowed
3. Duplicate statements are not allowed
let、const and var What's the difference? ?
One 、let command
1. Basic usage
es6 Added let command , Is used to declare variables . Usage and var similar , But the declared variable is only in the let Where the command is located Within code block It works .

Above picture , stay Code block {} In , Use them separately let and var Two variables declared . Then call these two variables outside the code block ,let Declared variable error ,var The declared variable returns correctly . indicate ,let Declared variables are only valid in the code block in which they reside .
for(let i = 0;i<10;i++){};
console.log(i); // ReferenceError: i is not defined
// In the above code ,let Declarative i Only in for Circulating efficiency , If it is used outside the circulation, it will report an error .
The following code uses let and var To declare the counter i, The final output is different .
var arr = [];
for (let i = 0; i < 10; i++) {
arr[i] = function () {
console.log(i);
};
}
arr[4](); // 4
var arr = [];
for (var i = 0; i < 10; i++) {
arr[i] = function () {
console.log(i);
};
}
arr[4](); // 10 Variable i Yes, it is var command-declared , Valid globally , Each cycle , Global variables i Will change . in other words , Array arr In every member of i All point to the same i( That is the last round i Value ), That is to say 10.
Variable i yes let Declarative , Current i Only valid in this cycle , So every cycle of i It's actually a new variable , Initialize variables of this round i when , On the basis of the last round of calculation .

in addition ,for The part of the loop that sets the loop variable is a parent scope , Inside the circulatory body is a separate sub scope .
The above code runs correctly , Output 5 Time abc. This means that the variables inside the function i And loop variables i Not in the same scope , Each has its own scope ( The same scope cannot be used let Repeatedly declare the same variable ).
namely let The declared variable has a block level scope , Valid only in the current scope .
2. Duplicate statements are not allowed
let Not allowed in the same scope , Repeatedly declare the same variable .var You can declare repeatedly within the same scope .
{
let a = 10;
let a = 20;
}
// Report errors Uncaught SyntaxError: Identifier 'a' has already been declared
{
var b = 10;
var b = 20;
} // Don't complain 3. No variable promotion
Variable Promotion It means that variables can be used before declaration , The value is undefined.var Declared variables have variable promotion , Before declaring, you can use , No mistake. .
{
console.log(str); // Output underfined
var str = 'var Statement ';
}
{
console.log(bar); // Report errors ReferenceError
let bar = 'let Statement ';
} In the above code , Variable str use var A statement of order , Variable promotion will occur , When the script starts to run , Variable str It already exists , But it's not worth it , So it will output undefined. Variable bar use let A statement of order , No variable Promotion . This means that before declaring it , Variable bar It doesn't exist , If you use it , Will throw a mistake .
namely let The declared variable does not have a variable promotion , Only after declaration can you use ,var There are variable promote
Two 、const command
1. Basic usage
const Declare a read-only constant . Once declared , You can't change the value of a constant .
const str = '123';
str = '456';
//TypeError: Assignment to constant variable.
const name;
//SyntaxError: Missing initializer in const declaration
As shown in the above code , Changing the value of a constant will result in an error . const Once a variable is declared , It must be initialized immediately , It cannot be left for later assignment . Only declare no assignment , You're going to report a mistake .
namely const Used to declare a constant , Must assign immediately . You can't change its value
If const What is declared is data of composite type ( Mainly objects and arrays ), The variable points to the corresponding memory address , Only the address corresponding to this pointer can be guaranteed to remain unchanged , As for its internal elements, they are variable .
const obj = {name:' Xiao Ming '}
obj.age = 20;
obj = {name:' Little '}
// Uncaught TypeError: Assignment to constant variable.
obj;//{name:' Xiao Ming ',age:20}namely const Declare compound constants , The address pointed to does not change , Its internal properties can be changed .
2. Block level scope
const The scope of let The same command : Valid only in the block level scope where the declaration is located .
{
const value= '123';
}
value; //VM2767:1 Uncaught ReferenceError: value is not definednamely const The declared variable has a block level scope , Valid only in the current scope
3. Duplicate statements are not allowed
const And let The same command : Cannot be declared repeatedly in the same scope .
var message = 'hello';
const message = '1';
//SyntaxError: Identifier 'message' has already been declarednamely const Not allowed in the same scope , Repeatedly declare the same variable
4. No variable promotion
const And let The same command : Variable cannot be used before declaration , Will report a mistake .
{
console.log(name);
const name=" Xiaoli ";
//Uncaught ReferenceError: Cannot access 'name' before initialization
}namely const The declared variable does not have a variable promotion , Only after declaration can you use .
summary
let、const and var What's the difference? ?
- let and var Are used to declare variables .const Is used to declare constants .
- const Must be declared with a value , Its value cannot be changed later . What is declared is that a compound variable can change the value of its internal child elements .
- let and const The declared variable has a block level scope , Valid only in the current scope .var There is no block level scope .
let and constNot allowed in the same scope , Repeatedly declare the same variable .var You can declare repeatedly within the same scope .- let and const The declared variable does not have a variable promotion , Only after declaration can you use , Otherwise, an error will be reported .var There are variable promote , Before declaration, you can use the value undefined.
边栏推荐
- 2022年暑假ACM热身练习1(总结)
- Moveit2——1.开始
- Cve-2022-34265 Django extract & TRUNC SQL injection vulnerability recurrence
- 缤纷彩色文字广告代码,文字广告代码美化版,给网站添加文字广告教程
- uni-app微信小程序——商城(6)——我的主页
- 微信推送-模版消息参数配置
- 智能指针(shared_ptr、unique_ptr、weak_ptr)
- let和const、let、const和var的区别
- Use bat to automatically execute CMD commands (multiple commands or a single command)
- Replace special characters in URL (%e2%80%8b)
猜你喜欢

微信推送-模版消息参数配置

js截取字符串前几位或者截取字符串后几位

wget 警告: 无法验证

qs模块是?

服务器如何安装宝塔面板?(宝塔面板安装教程)

How does the website count the number of visitors? How to install and use 51la?

uni-app微信公众号(4)——地址管理页面
![Understand PHP from [Fifth space 2021] easycleanup_ session](/img/fc/95332d488dd6096f3a3f6a9fb11644.png)
Understand PHP from [Fifth space 2021] easycleanup_ session

Uni app wechat applet - Mall (6) - my home page

Use leaflet to copy the original shentiwa Mega map to make a diary
随机推荐
v-on的修饰符
NodeJS 跨域 CORS
(八)Shell函数
“我的”Bug大全(转载)
(三)传递参数
Use bat to automatically execute CMD commands (multiple commands or a single command)
uni-app微信小程序——商城(6)——我的主页
小程序swiper高度
Moveit2——1.开始
04_理解MVVM
Replace special characters in URL (%e2%80%8b)
Text indent in uniapp doesn't work, and the indentation in the first line of uniapp doesn't work. How to solve it?
elemtnui 表格如何修改某行文字颜色(elemtnui table 修改某行文字颜色)
自己封装的风格化的开关卡片组件
es6 map提取数组对象
2022年暑假ACM热身练习1(总结)
XSS simple summary
Uniapp calls the map to query the location and mark the location
C Programming Language(2nd Edition)--读书笔记--1.5.1
JS高阶函数 filter/map/reduce