Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A javascript 'let' global variable is not a property of 'window' unlike a global 'var' [duplicate]

I used to check if a global var has been defined with:

if (window['myvar']==null) ...

or

if (window.myvar==null) ...

It works with var myvar

Now that I am trying to switch to let, this does not work anymore.

var myvar='a';
console.log(window.myvar); // gives me a
let mylet='b';
console.log(window.mylet); // gives me undefined

Question: With a global let, is there any place I can look if something has been defined like I could with var from the window object?

More generally:
Is var myvar='a' equivalent to window.myvar='a'?
I hear people say that at the global level, let and var are/behave the same, but this is not what I am seeing.

like image 684
Fai Ng Avatar asked Sep 09 '16 15:09

Fai Ng


People also ask

Can JavaScript let 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.

Can I use let as global variable?

Variables declared outside of any functions and blocks are global and are said to have Global Scope . This means you can access them from any part of the current JavaScript program. You can use var , let , and const to declare global variables.

Is window a global variable?

In HTML, the global scope is the window object. All global variables belong to the window object. Your global variables (or functions) can overwrite window variables (or functions).

What is global property in JavaScript?

The global property specifies whether or not the "g" modifier is set. This property returns true if the "g" modifier is set, otherwise it returns false.


1 Answers

I hear people say that at the global level, let and var are/behave the same, but this is not what I am seeing.

Eh, yes and no. To answer your question, in the large majority of cases let and var are the same when declared globally, but there are some differences.

To explain: A global environment record is where JS keeps all the logic and memory values for the code in play. However, this global environment record is really a composite encapsulating an object environment record and a declarative 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 record whose "binding object" is the global object. This would be the Realm, which in most cases is the window object.

So, per the ECMA spec, only FunctionDeclarations, GeneratorDeclarations, and VariableStatements create bindings in the global environment's object environment record.

All other declarations (i.e const and let) are stored in the global environment's declarative environment record, which is not based on the global object. They still declare a variable, but are not a VariableStatement.

TL;DR: Both are still global, but vars are stored in the window object, while lets are stored in a declarative environment that you can't see (just like you can't access a variable in a function scope from outside of the function). When declared globally, both statements are pretty much identical in use.

like image 50
joh04667 Avatar answered Sep 23 '22 13:09

joh04667