Detailed explanation and difference of let const command in ES6

Detailed explanation and difference of let const command in ES6

One. let command

  1. The let command is used to declare variables, and its usage is similar to var, but the declared variables are only valid in the code block where the let command is located
        {
            let a = 1;
            var b = 0
        }
        console.log(a); //因为在a所在的块级作用域之外访问,所以会抛出错误
  1. There is no variable promotion for let, and variable promotion for var
        console.log(temp); //undefined
        var temp = 10;//var存在变量提升,所以不会抛出错误
        console.log(temp1); //Error,因为let不存在变量提升,所以要先定义,再使用
        let temp1 = 10;
  1. Temporary Dead Zone (TDZ): As long as there is a let command in the block-level scope, the variables it declares are "bound" to this area, and are no longer affected by external influences. In short: inside the code block, use let declarations Before the variable, the variable is unavailable
		var temp2 = 123;
        if (true) {
            temp2 = 'abc';
            let temp2;
        }
        console.log(temp2);//因为在声明temp2之前就使用了temp2,根据let的暂时性死区,会抛出错误

The temporary dead zone also means that typeof is no longer a 100% safe operation

		typeof(x); //Error
        let x;//因为暂时性死区而没有检测到x的值,所以会抛出错误

As a comparison, if a variable is not declared, using typeof will not cause an error, but will return undefined, so before there is no let, there will be absolutely no error using typeof

        console.log(typeof(y)); //undefined
  1. let does not allow multiple declarations of the same variable in the same scope
        function fn1() {
            let a = 10;
            var a = 11;
        }
        fn1();//Error
        function fn2(){
            let a=10;
            let a=11;
        } 
        fn2();//Error

Two. const command

  1. const declares a read-only variable. Once declared, the value of the constant cannot be changed
        const PI = 3.14;
        console.log(PI);
        PI = 3.1415926;//Error,因为已经给PI赋值成3.14,所以不允许再对PI进行改动

This also means that once const is declared a constant, it must be initialized immediately and cannot be left for later assignment, that is to say, an error will occur if only declaring a constant without assigning it

const foo;//Error,因为没有给foo赋值
  1. The scope of const is the same as let, and is only valid in the block-level scope where the declaration is located (the same point of const and let)
        if (true) {
            const MAX = 99;
        }
        console.log(MAX);//Error,在块级作用域之外访问MAX会抛出错误
  1. Variables declared by const will not be promoted. There is also a temporary dead zone, which can only be used after declaration (the same points as const and let).
    Refer to the above let code
  2. const is the same as let, and cannot be declared repeatedly (the same points between const and let)
    refer to the above let code
  3. What const actually guarantees is not that the value of the variable cannot be changed, but that the memory address pointed to by the variable cannot be changed. For simple types of data (numbers, strings, Boolean values), the value is stored in the memory address pointed to by the variable Therefore, it is equivalent to a constant, but for compound data types (objects, arrays), the memory address pointed to by the variable is only a pointer. const can only guarantee that the pointer is fixed. As for whether the data structure it points to can be This is completely uncontrollable, so be careful when declaring an array or object as a constant
        const obj = {};
        obj.name = 'jisoo';
        obj.age = 25;
        console.log(obj);//上述代码都没有问题,可以给对象进行属性的添加操作
        obj={};//Error,因为不能将obj指向另外一个对象
        const arr = [];
        arr.push('blackpink'); //blackpink
        console.log(arr.length); //1上述代码都没有问题,因为数组是可读写的,可以添加新的元素给数组
        // arr = ['jisoo'];//Error,因为不能将此数组赋值给另外一个数组

Guess you like

Origin blog.csdn.net/Angela_Connie/article/details/110756948