Salesforce Certified JavaScript Developer Practice Questions

Salesforce Certified JavaScript Developer exam preparation

Salesforce Certified JavaScript Developer practice questions in English: 180 questions with explanations. The mock exam has 60 questions and 105 minutes.

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?

  1. EMEA undefined, because only the var declaration becomes a property of the global object
  2. EMEA Dana, because top-level declarations of both kinds are copied onto the global object
  3. undefined undefined, because variables declared in a script are never reachable through the global object
  4. 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.

Go to the Salesforce Certified JavaScript Developer practice app (10 free questions; purchase unlocks all questions)

See the full practice set