Salesforce Certified JavaScript Developer free question 1 / 10 · Domain 1: Variables, Types, and Collections
In a classic browser script, a developer declares two top-level variables. var region = 'EMEA'; let owner = 'Dana'; console.log(globalThis.region, globalThis.owner); What is logged?
- EMEA undefined, because only the var declaration becomes a property of the global object
- EMEA Dana, because top-level declarations of both kinds are copied onto the global object
- undefined undefined, because variables declared in a script are never reachable through the global object
- undefined Dana, because let is the keyword that publishes a name globally while var keeps it private to the script
Show the answer and explanation
Answer: A
Why this is correct
The let page lists, as one difference from var, that a top-level let declaration in a script does not create a property on globalThis. The var page shows the other side: after a top-level var x = 1, Object.hasOwn(globalThis, 'x') is true. So globalThis.region is EMEA while globalThis.owner is undefined.
Why the other options are wrong
"EMEA Dana, because top-level declarations of both kinds are copied onto the global object" … The page says let declarations do not create properties on globalThis at the top level of a script. Only var does.
"undefined undefined, because variables declared in a script are never reachable through the global object" … The var page shows that a top-level var does create a property, which Object.hasOwn(globalThis, name) reports as true.
"undefined Dana, because let is the keyword that publishes a name globally while var keeps it private to the script" … This swaps the two keywords. let keeps a script-level name off the global object, and var is the one that adds it.
Key points
The global object only picks up top-level var declarations. let and const names remain reachable by their plain names but are not properties of globalThis.