Are variables declared with let or const hoisted?

前端 未结 6 995
爱一瞬间的悲伤
爱一瞬间的悲伤 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:23

    Quoting ECMAScript 6 (ECMAScript 2015) specification's, let and const declarations section,

    The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s LexicalBinding is evaluated.

    So, to answer your question, yes, let and const hoist but you cannot access them before the actual declaration is evaluated at runtime.

提交回复
热议问题