Ian Halliday (2013-11-08T23:35:46.000Z)
domenic at domenicdenicola.com (2013-11-13T16:53:42.365Z)
I'm having difficulty figuring out where the ES6 draft spec specifies that a "use before declaration" error should be thrown. My last understanding of the temporal dead zone was that ECMAScript would always issue a "use before declaration" error at runtime, regardless whether it can be statically determined or not. However it seems like the evaluation semantics of var declarations, for example, do not lead to any line that throws a ReferenceError. That is, consider this code: ```js function f() { { var x = 5; let x; } } f(); ``` I think the var declaration creates a binding for x in the function's lexical environment, but then binds to the x in the block's environment for the initialization. As such, the initialization should throw a "use before declaration" error. But this is what I cannot find in the spec. Maybe I am wrong about the semantics here? If I am not wrong then this raises the question of order of operations. Is the RHS evaluated first, then the error is thrown? Or is the LHS name evaluated first, throwing if it is a binding that is currently in its TDZ? E.g. is g() called here before throwing? ```js function g() { console.log('hello'); return 0; } function f() { { var x = g(); let x; } } f(); ``` How about for-in/of loops? Is g() called here? ```js function g() { console.log('hello'); return { a: 1, b: 2, c: 3 }; } function f() { { for (var x in g()) { console.log('unreachable'); } let x; } } ```