A variable is declared with var inside an if block in a function. Where can it be used?
var is function-scoped: blocks such as if, for and { } do not limit it. Declared at the top level of a script, it would be global.
Variables: var, let and const · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript variables: function and block scope, hoisting, the temporal dead zone, redeclaration, const objects, Object.freeze and loop bindings. Pick an option to check your answer instantly.
A variable is declared with var inside an if block in a function. Where can it be used?
var is function-scoped: blocks such as if, for and { } do not limit it. Declared at the top level of a script, it would be global.
What is the scope of variables declared with let and const?
let and const are block-scoped: an if, for, while or plain { } block limits them.
Which keyword allows the same variable to be declared twice in the same scope without an error?
var x = 1; var x = 2; is legal. Doing the same with let or const is a SyntaxError (Identifier has already been declared).
What exactly does const guarantee for const team = ["Asha", "Ravi"];?
const makes the binding constant. team.push("Meera") works; team = [] throws TypeError. Use Object.freeze to block changes to the array itself.
What is the temporal dead zone (TDZ)?
let and const are hoisted but stay uninitialised until their declaration is evaluated. Any access in the TDZ – even typeof – throws "Cannot access 'x' before initialization".
Which statement about hoisting is correct?
All declarations are registered before the scope runs. That is why a block-level let shadows an outer variable even before its own line – and why using it there throws instead of reading the outer value.
In a classic browser script (not a module), which top-level declaration creates a property on window?
Top-level var and function declarations become properties of the global object (window.appName). let, const and class declarations live in a separate global lexical scope. Modules create no globals at all.
Which declaration is a SyntaxError when written without a value, e.g. ___ total;?
const total; is "Missing initializer in const declaration". let total; and var total; are fine and hold undefined.
const cfg = Object.freeze({ db: { host: "local" } }); Which change still takes effect?
Object.freeze is shallow: cfg's own properties cannot be changed, added or deleted, but the nested db object is not frozen, so its host can change.
Which declaration style do modern style guides recommend?
const documents intent and prevents accidental reassignment; let covers counters and accumulators. ESLint's prefer-const and no-var rules enforce this.
Timed online tests with instant scores and explanations.