2

I just played around with global declerations using var, const, and without any, and noiticed that while var and without can be accessed using globalThis, const cant.

aaa = "aaa";
var bbb = "bbb";
const ccc = "ccc";
console.log(globalThis.aaa, globalThis.bbb, globalThis.ccc); // logs: aaa bbb undefined

My question is, is there a way to access them using window or globalThis? And if not where else are they?

As they are global there is not really any practical need for this, I'm just curious.

  • "is there a way to access them using window or globalThis?" nope. That's the whole purpose of `let` and `const`. – Jonas Wilms Jul 24 '21 at 19:36

1 Answers1

2

Variables declared with var and without any keyword are attached to the global object.

My question is, is there a way to access them using window or globalThis?

Yes, if you create a new property on the global object pointing to your const. But why do that.

And if not where else are they?

They are variables just like globalThis. Not attached to anything, can be accessed directly. If you are talking about the scope then they are block scoped. Live inside the { } they are defined in.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39