The difference and usage of var, let, const in js

The difference and usage of var, let, const in js

After ES6 came out, block-level scope was added to js, ​​which was not in the original ES5, and also derived two new variable declaration methods, let and const.

How to write var in ES5

function add(num1, num2){
    
    
	var sum = num1 + num2;
	return sum;
}
var result = add(10, 20);//这是输出的是30
alert(sum);//但是这里的调用是完全不行的

In other words, in the original es5, js will add the declared variable to the latest execution environment, which is the sum defined in the function, so the sum here will be added to the part of the function In the environment, it is of course impossible to make calls later. But the suffocating operation is below:

for(var i = 0; i < 10; i++){
    
    
	...
}
alert(i)//i = 10,

It can be seen that since the current execution environment is the global environment, js directly adds the defined variable i to the global environment, so after the for loop, the i defined in the loop can still be operated.

Let and const in ES6

The newly added let and const in ES6 both support block-level scope

let

The biggest difference between let and var is that they support block-level scope, but there are still other differences:
(1) If let is declared in the global environment, it is a global variable, and declared in a local environment is a local variable (in {} Valid, {} is invalid)

let i = 2;
for(let i = 0; i < 10; i++){
    
    
	...
}
alert(i)//这里输出的依旧是2

(2) Let can only be declared first and then called; var can be called first and then declared

//var这样用是可以的:
name = "asu";
var name;

//但是这样使用let是不可以的,let一定需要先声明
name = "asu";
let name;//错误的

(3) Variables declared by let in the same scope cannot be reset, that is, they cannot be declared repeatedly; but var can reset previously declared variables

//var可以重置声明的变量:
var x = 3;
var x = 2;
alert(x)/这里输出的是2

//let则是不可以重置已经声明的变量的:
let y = 2;
let y = 3;//error,这是错误的
if(true){
    
    
	let y = 5;
	alert(y)//在局部环境中声明,环境不一样,输出5
}
alert(y);//依旧输出的是2

Usage of const

Let me first talk about the difference between let and const
(1) Let is not initialized when it is declared, and const must be initialized when it is declared.

//这样声明是正确的
var name = "LiHua";

//声明的时候没有初始化是错误的:
var city;
city = "wuhan";//error

(2) Variables declared by const cannot be modified, but this is not an absolute meaning. The above cannot be modified. Note that the value of const-defined constants cannot be assigned and modified, but const-defined arrays and objects can be modified. For adding and modifying the elements, the most important thing is that the defined arrays and objects cannot be reassigned ; the values ​​of the variables defined by let can be modified.

const names = ["Bob", "Kathy", "LiHua"];
const person = {
    
    name:"LiLei", age:20};
//添加元素和修改元素都是可以的
names[0] = "Li";
names.push("Pob")
person.age = 21;
person.gender = "female";

//但是重新给数组和对象进行赋值是错误的
names = ["asu", "LiLi"];//error
person = {
    
    name:"Bob", age:18};//error

The same is:
(1) In the same scope, repeated declarations are not allowed, and declarations cannot be reset.
(2) Global variables declared with let and const are not owned by window.

Guess you like

Origin blog.csdn.net/suandyanr/article/details/106671856