What is the temporal dead zone?

后端 未结 3 1163
甜味超标
甜味超标 2020-11-21 10:10

I\'ve heard that accessing let and const values before they are initialized can cause a ReferenceError because of something called the

3条回答
  •  野的像风
    2020-11-21 10:47

    In case of let and const variables, Basically, Temporal Dead Zone is a zone

    "before your variable is declared",

    i.e where you can not access the value of these variables, it will throw an error.

    ex.

    let sum = a + 5;        //---------
    //some other code       //         | ------>  this is TDZ for variable a
                            //         |
    console.log(sum)        //---------
    let a = 5;
    

    above code gives an error

    the same code will not give an error when we use var for variable 'a',

    ex.

    var sum = a;                            
    console.log(sum)     //prints undefined
    var a = 5;
    

提交回复
热议问题