ReferenceError does not throw when accessing 'let' variable before declaration

I tried to execute the code below in Firefox V30.0 Scratchpad:

function do_something() { console.log(foo); // ReferenceError let foo = 2; } do_something(); 

The expected behavior is that my program should call the Error link, because I access the let variable before the declaration. But, I do not get the expected behavior, the program was executed, and the result is lower

 undefined 

Can you explain to me why this is so?

+5
source share
2 answers

According to the MDN compatibility table , Firefox does support semantics of the temporary dead zone only with v35.

Also, you should always be sure to use strict mode. Some ES6 features are not available in sloppy mode due to concerns about breaking an outdated network. It should not affect this particular case, even though Firefox already has a long history of using let .

+6
source

Let the variables in ES6 go up to the top of the block where they are declared. Linking a variable before its declaration will result in a ReferenceError ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let ). So you are right to expect that in this case a ReferenceError will fire.

The reason the ReferenceError does not occur in this case is because the FF 30 does not support the so-called "temporary dead zone". A good place to find out if the browser supports certain parts of the ES6 specification is the Kangax Ecmascripts card compatibility table ( https://kangax.imtqy.com/compat-table/es6/#test-let ).

+4
source

Source: https://habr.com/ru/post/1237918/


All Articles