Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const variable not hoisted for immediately invoked function

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.

like image 384
years_of_no_light Avatar asked May 27 '15 09:05

years_of_no_light


People also ask

Are IIFEs hoisted?

Unlike function declarations, function expressions are not hoisted. These functions are only available when the JavaScript interpreter processes that line of code. Another difference between function expressions and function declarations is that function expressions can define functions without a name.

What is the use of immediately invoked function expression?

An Immediately-invoked Function Expression (IIFE for friends) is a way to execute functions immediately, as soon as they are created. IIFEs are very useful because they don't pollute the global object, and they are a simple way to isolate variables declarations.

Can you explain IIFE with code?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog.

Are constants hoisted?

Variables declared with let and const are also hoisted but, unlike var , are not initialized with a default value.


2 Answers

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.

like image 160
Yuvraj Avatar answered Nov 13 '22 08:11

Yuvraj


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();
like image 22
Guffa Avatar answered Nov 13 '22 10:11

Guffa