4

If global variables defined with const or let are not stored on window, where are they being stored?

var varTest = 'var test';
const constTest = 'const test';
let letTest = 'let test';

varTest            //"var test"
letTest            //"let test"
constTest          //"const test"
window.varTest     //"var test"
window.constTest   //undefined
window.letTest     //undefined
  • 3
    Variables declared with `let` or `const` are not global. They are declared in the module scope. – zerkms Jun 06 '17 at 03:41
  • 4
    It stores them on yellow post-its stuck on the refrigerator door. –  Jun 06 '17 at 03:42
  • Does it matter? I thought they were deliberately not made available via a global object. – nnnnnn Jun 06 '17 at 03:45

1 Answers1

6

A global environment record consists of two parts

Environment records are conceptional data structures for storing the identifier name -> value mappings.

As you might suspect, object environment records are backed by actual user space objects, such as the global object or an arbitrary object when you use with. That's what makes global bindings become properties of the global object.

let, const and other declarations are store in the declarative environment record part which is backed by some implementation specific data structure. You have encountered declarative environments before because every function environment is a declarative environment. So one could also say "let and const are stored in the global scope the same way as any binding is stored in a function".

From the spec:

A global Environment Record is logically a single record but it is specified as a composite encapsulating an object Environment Record and a declarative Environment Record. The object Environment Record has as its base object the global object of the associated Realm. This global object is the value returned by the global Environment Record’s GetThisBinding concrete method. The object Environment Record component of a global Environment Record contains the bindings for all built-in globals (clause 18) and all bindings introduced by a FunctionDeclaration, GeneratorDeclaration, or VariableStatement contained in global code. The bindings for all other ECMAScript declarations in global code are contained in the declarative Environment Record component of the global Environment Record.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143