当前位置:网站首页>Differences among let, VaR and const when JS declares variables
Differences among let, VaR and const when JS declares variables
2022-06-13 08:24:00 【tingkeaiii】
let、const yes es6 The new method of defining variables .let Has been widely used , It can make up for var Some deficiencies in use .
1.var
- var The scope of is global scope and function scope ( Local )
- var Declared variables , Variable values can be modified , Can be restated
var v2 = 1;
var v2 = 2;
console.log(v2); /*2,var Can be declared repeatedly */
- Use var Variables defined , It will automatically become window Object properties , Both in the global scope and in the local scope .let、const Can't
var v2 = 2;
console.log(window.v2); /*2,var Global variables are automatically added window Properties of variables */
let v3 = 1;
console.log(window.v3); /*undefined*/
- var When you're ascending , Declarations and functions are mentioned at the top of the scope , And assign... To the variable undefined The initial value of the .
function func() {
console.log(name);
var name = 'fkit';/*var name Advance and assign initial value , Output undefined*/
console.log(name);
}
3. let
let A scope is a block level scope ,{ } Inside the braces is a piece , Only valid in the current block ;
- let The defined variables can be modified , But it cannot be defined more than once in the same scope ,
let v1 = 1;
v1 = 2; /* Change the value of the variable */
console.log(v1); /*2, Can be modified within a scope */
let v1 = 3;
console.log(v1); /* Report errors , A scope cannot be declared repeatedly */
The same variables can be defined in different scopes , Mutual interference . Because they are in different scopes .
let v1 = 1;
console.log(v1); /*1*/
if (v1 === 1) {
let v1 = 2;
console.log(v1); /*2*/
}
- let The variable of .let The declaration of is also promoted to the top of the scope , But it will not undefined assignment . If it is used without assignment, an error will be reported
function func() { console.log(name); /* Not stated in advance name, Report errors */ let name = 'fkit'; console.log(name); } func();
3. const
- const It's a block level scope
- const Cannot be modified and cannot be redeclared , It is equivalent to defining a constant .
const v4 = 1;
v4 = 3;/* Attempt to modify value */
console.log(v4);/* Report errors */
const v4 = 1;
const v4 = 2;
console.log(v4); /* Report errors */
- All say const The defined value cannot be modified , In fact, the address of the defined value cannot be modified . Because when const When complex data types are defined , You can also manipulate his elements .
When using const When defining objects , You can use object names . attribute = Modify the attribute value of an object by attribute value .
const obj = {
times: 4,
};
obj.times = 5;
- const The variable of , And let equally , Promote to the top of the scope , But not initialized . If the definition is used without assignment, an error will be reported
边栏推荐
- 水仙花升级版(自幂数)
- 京东商品详情接口,京东详情页接口,宝贝详情页接口,商品属性接口,商品信息查询,商品详细信息接口,h5详情,京东APP详情,京东api接口,京东历史价格数据接口代码对接分享
- HCIP_ MGRE experiment
- [game theory complete information static game] Application of Nash equilibrium
- 天猫商品详情接口,天猫商品优惠券接口,天猫api接口,天猫价格监控接口,天猫比价接口,品牌维权接口,天猫销量api接口,接口代码可对接数据分析业务,品牌维权,比价业务,行业分析业务接口代码分享
- What software can be used to solve the problems faced by the auto parts industry
- [virt manager] remote management the problem of the floating mouse when starting the virtual machine
- How about a well-known food material distribution information management system?
- How to install the bdtab (BD) new tab plug-in in edge browser (Graphic tutorial)
- Which is the stronger fresh food distribution and sorting management system?
猜你喜欢
Remote access and control
Methods of importing and exporting settings in Altium Designer
Guidance process and service control
How to efficiently manage commodities and inventory in the beverage wholesale industry
Process and scheduled task management
ERP basic data Kingdee
MySQL sorts according to the specified order of the specified fields
MySQL parsing serialized fields
Using KVM to create three virtual machines that can communicate with local area network
Buuctf web (VII)
随机推荐
Import the robot model built by SolidWorks into ROS
PHP isset() method ignores data error handling caused by null parameter value
Using KVM to create three virtual machines that can communicate with local area network
Motiko basic syntax in dfinity (ICP) -8
Introduction to dfinity (ICP) -1
Determine whether a string is rotated from another string
Guidance process and service control
CCNP_ Bt- Reissue
CCNP_ BT-MGRE
Differences between Merkle DAG and Merkle tree
How to use annotations in word
Redis subscribe connection timeout interrupt problem solution
Batch package and download Alibaba OSS files
[virt manager] remote management the problem of the floating mouse when starting the virtual machine
判断一个字符串是否由另外一个字符串旋转而来
Edge browser uses bdtab new tab plug-in (BD new tab)
ERP basic data concept
Logstash configuration reference article
Shell脚本常用开发规范
LVM management exercise