I was playing around new ECMASCRIPT-6 const key word. I did not understand one particular behaviour of the keyword.
Lets say I have two functions
First case
(function(){
console.log(_t);
const _t=10;
})();
and Second case
function t(){
console.log(_y);
const _y=11;
}
t();
For the first case the output is (didn't understand why)
ReferenceError: can't access lexical declaration `_t' before initialization
For the second case the output is (fine)
undefined
The second case output is as expected but I'm not getting any idea why the first case result throws error. It can be inferred from the error that the variable is not hoisted. But why? I found here that const
uses block scope. Has it anything to do with this scoping?
I'm using Firefox Developer Version console to run tests.
_y
in an outer scope (in other code that ran), does the same thing happen when you run only these two snippets in a clean environment? – Parget