[Javascript] let doesn't hoist -- false

"let" keyword, the same as "const" does hoist the variable. But different from 'var', it does both hoist and assign value as "undefined".

"let" in other hand, it just hoist the variable, but doesn't assign and value until runtime code is evaluated. 

If you using let variable before you define it, it will throw TDZ error.

{
   food = 'apple' // TDZ error!
   let food;
}

Best way to avoid all TDZ error, is always define 'let/const' in the first line of the file.

{

   let food;


   ... 

   ...


   food = 'apple'

}
原文地址:https://www.cnblogs.com/Answer1215/p/12342739.html