1
  1. Open Google Chrome's dev console.
  2. Enter const a = "foo";

Unlike entering var b = "bar"; which will show as a property of window, viewable as window.b, there is no window property 'b'.

Const has block scope instead of function scope, but I'm curious why var would be attached to the window object while const would not.

I haven't been able to find any explanation for this behavior.

Don P
  • 60,113
  • 114
  • 300
  • 432
  • 1
    [MDN const Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) state `Global constants do not become properties of the window object, unlike var variables`. One thing to remember in history of javascript is the whole language was created in just a few weeks to satisfy needs set by NetScape browser. As a result it had lots of quirks – charlietfl Mar 27 '21 at 17:51
  • Right, Javascript started somewhat badly designed, revisions like `let` and `const` are intended to fix that as much as adding new features. So the answer on one level is: because that’s how it was designed, on purpose. The deeper technical specification can be found in the duplicate(s). – deceze Mar 27 '21 at 18:14

0 Answers0