当前位置:网站首页>Let, const, VaR in ES6
Let, const, VaR in ES6
2022-07-19 10:05:00 【qq_ forty-two million forty-two thousand one hundred and fifty-】
Basic use
- var Declare variables
- let Instead of var, Declare variables
- const declare constant
const Detailed explanation
const It is designed for situations where you don't want to re assign values once initialized
const Precautions for
1. Use const declare constant , Once declared , It must be initialized immediately , It cannot be left for later assignment
// Wrong writing
const sex;
sex='male';
// Write it correctly
const sex = 'male';
2、const Declared constant , If it is a reference data type, it is allowed to No reassignment To modify its value , But the basic data type cannot
for example
const person = {
username: 'Alex' };
// person = {}; error
person.username = 'ZhangSan';
console.log(person);

let、const And var The difference between
1. Repeat statement
A variable or constant that already exists , Once again
var Allow duplicate statements ,let、const Don't allow
// Will report a mistake
function func(a) {
let a = 1;
}
func();
2. Variable Promotion
var Will raise the declaration of variables to the top of the current scope
for example
console.log(a);
var a = 1;
amount to
var a;
console.log(a);
a=1;

let、const No variable promotion
for example
console.log(a);
let a = 1;

3. Temporary dead zone
As long as... Exists within the scope let、const, The variables or constants they declare are automatically “ binding ” This area , No longer affected by external scopes
for example
let a = 2;
function func(){
console.log(a);
let a = 1;
}
func();
The variables in the function have been bound in the function a, Therefore, external variables cannot be read a, So it will report a mistake 
4、 Block level scope ( The most important difference )
var There is no block-level scope
for (var i = 0; i < 3; i++) {
console.log('infor--'+i);
}
console.log(i);
Variable i It's only used to control the cycle , But at the end of the cycle , It's not gone , Due to variable Promotion , Leakage becomes a global variable .
let/const There are block-level scopes
for (let i = 0; i < 3; i++) {
console.log('infor--'+i);
}
console.log(i);
i Only in for Effective in circulation 
What block level scopes are there
- {}
- for(){}
- while(){}
- do{}while()
- if(){}
- switch(){}
let and const Use advice
Used by default const, Only when you know that the variable value needs to be modified let
边栏推荐
- Rocky基础之正则表达式
- [C language] shallow selection, circular statements, functions and arrays
- SSH connection to Huawei modelarts notebook
- Conversion between two-dimensional array and sparse array
- 浏览器的故事
- 金属有机骨架/碳化氮纳米片(UiO-66/HOCN)复合材料|MIL-101负载Au-Pd合金纳米粒子|化学试剂mof定制
- Redis cache avalanche
- Flink入门到实战-阶段五(处理函数)
- 【C语言】字符串、转义字符及注释
- Chapter 4 - consistency of first-order multi-agent systems - > consistency of continuous time systems with time delays
猜你喜欢
[email protected](Fe)复合纳米材料"/>金属有机骨架材料/聚合物复合材料ZIF-8/P(TDA-co-HDA)|氧化锌[email protected](Fe)复合纳米材料

UiO-66-(COOH)2改性聚酰胺纳滤膜|ZIF-8/PVP复合纳米纤维膜|UiO-66-NH2改性聚酰胺纳滤膜

Rhcsa jour 2 7,15
[email protected]@ZIF67纳米材料"/>氮杂环分子改性UiO-66-NH2|聚乙烯亚胺改性UiO-66-NH2|[email protected]@ZIF67纳米材料

第1周学习:深度学习入门和pytorch基础

Build a server environment with node+express

状态码的故事

Huawei Shengsi mindspire detailed tutorial
二维数组与稀疏数组之间的转换

第4章-一阶多智体系统一致性 -> 切换拓扑系统一致性
随机推荐
npm使用
TP5 判断请求方式
Talking about the informatization planning of industrial enterprises
rhcsa 第二天 7.15
how to use culasLt
实验1:使用Matlab工具箱进行相机标定实验
数据库概述
UiO-66-(COOH)2改性聚酰胺纳滤膜|ZIF-8/PVP复合纳米纤维膜|UiO-66-NH2改性聚酰胺纳滤膜
Machine learning basics that can be easily introduced in 5 minutes
一种基于Grubbs和孤立森林的多维序列异常检测方法
Rhcsa jour 2 7,15
Flink introduction to actual combat - phase IV (time and window diagram)
Flink入门到实战-阶段五(处理函数)
CLWY权限管理(一)--- 项目搭建
金纳米粒子修饰MIL-101骨架材料(AuNPs/MIL-101)/负载COF-TpPa-1(Au NPs/COF-TpPa-1)|齐岳试剂
On the problem of dependency invalidation when the dependency in the basic module is inherited by the sub module in the microservice
mof定制产品|N-K2Ti4O9/g-C3N4/UiO-66三元复合材料|纸基Au-AgInSe2-ZIF-8纳米复合材料
Clwy permission management (III) -- user group module
Excel数据插入Mysql数据库可能遇到的问题
rhcsa 第二天 7.15