Are variables declared with let or const hoisted?

前端 未结 6 999
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 04:58

I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected...

console.log(typeof name         


        
6条回答
  •  攒了一身酷
    2020-11-21 05:39

    From MDN web docs:

    In ECMAScript 2015, let and const are hoisted but not initialized. Referencing the variable in the block before the variable declaration results in a ReferenceError because the variable is in a "temporal dead zone" from the start of the block until the declaration is processed.

    console.log(x); // ReferenceError
    let x = 3;
    

提交回复
热议问题