const variable not hoisted for immediately invoked function
Asked Answered
F

2

10

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.

Fabrienne answered 27/5, 2015 at 9:34 Comment(3)
you have encountered the temporal dead zone, which is not yet completely implemented across browsers.Wilburn
See Are variables declared with let or const not hoisted in ES6? for how it should work. I cannot explain the second behaviour though - looks like a bug to me.Parget
Did you define any _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
A
4

This is Firefox related issue as mentioned in here

Firefox-specific notes

The const declaration has been implemented in Firefox long before const appeared in the ECMAScript 6 specification. For const ES6 compliance see bug 950547 and bug 611388.

Starting with Gecko 36 (Firefox 36 / Thunderbird 36 / SeaMonkey 2.33):

{const a=1};a now throws a ReferenceError and does not return 1 anymore due to block-scoping. const a; now throws a SyntaxError ("missing = in const declaration"): An initializer is required. const a = 1; a = 2; now also throws a SyntaxError ("invalid assignment to const a").

Also I found something here as well

I think Firefox engine is very strict on const hoisting.

I think this makes sense.

Armour answered 27/5, 2015 at 10:45 Comment(2)
This make sense. But this should have been applied for both of the cases. As during hoisting the declaration is hoisted only. And {const a; a=1} throws SyantaxError. But this should have been the case for both type of function declaration. And yes you are correct. This happens with firefox only. Chrome does not throw any exception.Fabrienne
I'm missing an explanation though why the behaviour (strict ES6 or some sloppy legacy-whatever stuff) is different for function declarations and IEFEs?Parget
R
2

When I try it in Firefox (38.0.1), I get the same error message for both; that it can't access it before initialization. That makes sense, as the only difference is that there is a function expression and a function declaration.

The constant identifiers are actually hoisted, that's why you get the error that it can't be accessed before it's initialized.

In this case block scope and function scope is the same, as the function code blocks are the only blocks that you have.

If you add a code block so that the constant is out of scope when you use it, you will get the error message "ReferenceError: _y is not defined" instead:

function t(){
  {
    const _y = 11;
  }
  console.log(_y); // _y is out of scope
}
t();
Relief answered 27/5, 2015 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.