Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do let statements create properties on the global object?

In JavaScript, var declarations create properties on the global object:

var x = 15; console.log(window.x); // logs 15 in browser console.log(global.x); // logs 15 in Node.js 

ES6 introduces lexical scoping with let declarations that have block scope.

let x = 15; {    let x = 14; } console.log(x); // logs 15; 

However, do these declarations create properties on the global object?

let x = 15; // what is this supposed to log in the browser according to ES6? console.log(window.x); // 15 in Firefox console.log(global.x); // undefined in Node.js with flag 
like image 396
Benjamin Gruenbaum Avatar asked Feb 27 '15 23:02

Benjamin Gruenbaum


People also ask

Does Let create a global variable?

Description. let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

What does the keyword let do?

The let keyword is used to declare variables in JavaScript. The var keyword can also be used to declare variables, but the key difference between them lies in their scopes. var is function scoped while let is block scoped - we will discuss this in more detail later.

What is the difference between LET and VAR?

let is block-scoped. var is function scoped. let does not allow to redeclare variables. var allows to redeclare variables.

What are global objects in JavaScript?

A global object is an object that always exists in the global scope. In JavaScript, there's always a global object defined. In a web browser, when scripts create global variables defined with the var keyword, they're created as members of the global object. (In Node. js this is not the case.)


1 Answers

Do let statements create properties on the global object?

According to the spec, no:

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.

Some more explanation:

  • A declarative environment record stores the bindings in an internal data structure. It's impossible to get a hold of that data structure in any way (think about function scope).

  • An object environment record uses an actual JS object as data structure. Every property of the object becomes a binding and vice versa. The global environment has an object environment object whose "binding object" is the global object. Another example is with.

Now, as the cited part states, only FunctionDeclarations, GeneratorDeclarations, and VariableStatements create bindings in the global environment's object environment record. I.e. only this bindings become properties of the global object.

All other declarations (e.g. const and let) are stored in the global environment's declarative environment record, which is not based on the global object.

like image 180
Felix Kling Avatar answered Oct 04 '22 11:10

Felix Kling