The difference between let, var and const in es6

The scope of the variable defined by var is the entire closed function, which is global. The scope of variables defined by let is at the block level or in sub-blocks.

 <script>
        console.log(fun); //undefined
        var fun=2; 
        console.log(fun); //2
        var fun=45; //不会报错
        let fun=46;//会报错
    </script>

Variable promotion phenomenon: The browser will pre-analyze before running the code. First, the function declaration will be parsed, the variable will be defined, and the function and variable will be run and assigned after the parsing.
-Regardless of which line the variable declared by var is in the current scope, it will be promoted to the head of the scope.
Variables declared by -var will be promoted to the top of the scope and initialized to undefined, while variables declared by let are not initialized at the top of the scope

<script>
        for (var i = 0; i < 5; i++) {
    
    
        }
        console.log(i);//5
        for (let j = 0; j <5 ; j++) {
    
    
        }
        console.log(j);//Uncaught ReferenceError: i is not defined
    </script>

Let keyword The
let keyword is used to declare variables. Variables declared using let have several characteristics:
1) Repeated declarations are not allowed
2) Block-level scope
3) There is no variable promotion
4) It does not affect the scope chain
application scenarios: It's right to declare variables later and use let

    <script>
    //声明变量
    let a;
    let b,c,d;
    let e = 100;
    let f = 521, g = 'iloveyou', h = [];

 	//变量不能重复声明
     let star = '罗志祥';
     let star = '小猪';

    // if else while for
    {
    
    
        let girl = '周扬青';//girl is not defined
    }
    console.log(girl);
	//不存在变量提升
    console.log(song);// Cannot access 'song' before initialization
    let song = '恋爱达人';

	//不影响作用域链
    {
    
    
        let school = '尚硅谷';
        function fn(){
    
    
            console.log(school);
        }
        fn();
    }

    </script>

2.2.
const keyword The const keyword is used to declare constants. The const declaration has the following characteristics:
1) The declaration must be assigned an initial value
2) The identifier is generally uppercase
3) Repeated declarations are not allowed
4) Values ​​are not allowed to be modified
5) Block-level functions Domain
Note: Object attribute modification and array element changes will not cause const error.
Application scenario: declare object type using const, non-object type declaration select let

		//声明常量
        const SCHOOL = '尚硅谷';
        //1. 一定要赋初始值
        const A;
        //2. 一般常量使用大写(潜规则)
        const a = 100;
        //3. 常量的值不能修改
        SCHOOL = 'ATGUIGU';
        //4. 块儿级作用域
        {
    
    
             const PLAYER = 'UZI';
        }
        console.log(PLAYER);//PLAYER is not defined
        //5. 对于数组和对象的元素修改, 不算做对常量的修改, 不会报错
        const TEAM = ['UZI','MXLG','Ming','Letme'];
        TEAM.push('Meiko');

Guess you like

Origin blog.csdn.net/qq_45731083/article/details/108582014