What happens when you declare var globally versus let or const?

In a classic browser script, global var creates a non-configurable window property. Global let/const use lexical bindings instead. Modules behave differently.
What's really being asked
Distinguish a global binding from a property on the global object, and identify the execution context.
The full answer
In a classic browser script, a top-level var declaration creates a non-configurable property on window. Top-level let and const are also globally scoped, but live in the global lexical environment and do not create window properties. Saying they are lexical bindings rather than window properties is a correct distinction, not a denial of global scope. var may be redeclared; let/const cannot be redeclared in the same scope and have a temporal dead zone before initialization. Module declarations have module scope; Node.js CommonJS top-level declarations are not browser globals. globalThis is useful for referencing the global object across environments. These extra details improve coverage; omitting them is not a false claim.
The mistakes people make
Explicitly saying all three create window properties. Explicitly saying top-level let and const can never have global scope. Explicitly saying let and const have no temporal dead zone or can freely be redeclared in the same scope. Do not infer any of these statements from a correct lexical-binding distinction.
A concrete example
In a classic script, var legacy = 1; let modern = 2; const fixed = 3;. window.legacy is 1; window.modern and window.fixed are undefined, while modern and fixed remain accessible as identifiers.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
Interview question
In a browser script, what distinguishes a top-level var declaration from let or const?
- a.let and const are not globally scoped, unlike var
- b.var creates a property on the global Window object, while let and const do not, though all three are globally scopedCorrect
- c.var is hoisted to the Window object, while let and const are not hoisted
- d.All three create Window properties, but only var allows reassignment via window.x
Why? this is the answer
var creates a writable property on the global Window object, while let and const are globally scoped without becoming Window properties. Option A is a tempting misconception because let and const are indeed globally scoped; they simply do not pollute the global object.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #javascript
- #global-scope
- #browser
- #var
- #es6
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on javascript — each one lists the topics its interview covers.
See open roles