JavaScript uses lexical scope. What does that mean?
A function sees the scopes that surround its definition. Calling it from a different place does not give it access to the caller's local variables.
Scope and Hoisting · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript scope and hoisting: kinds of scope, the scope chain, lexical scope, hoisting of functions, var, let, const and classes, execution contexts, closures, module scope and globalThis. Pick an option to check your answer instantly.
JavaScript uses lexical scope. What does that mean?
A function sees the scopes that surround its definition. Calling it from a different place does not give it access to the caller's local variables.
When a variable is used inside a nested function, in which order does the engine look for it?
The nearest declaration wins. If no scope declares the name, reading it throws a ReferenceError.
Which list contains only real kinds of scope in modern JavaScript?
Objects do not create scopes for variables; their properties are accessed through the object.
Which declaration is hoisted together with its value, so it can be fully used before its line?
Function declarations are created in full during the creation phase. var is hoisted as undefined; let and class are hoisted but uninitialised.
What happens when code runs new Car() on a line before class Car {}?
Class declarations behave like let: they are registered when the scope starts but stay in the temporal dead zone until the declaration runs.
What happens in the creation phase of a function's execution context?
After the creation phase, the execution phase runs the body line by line. Hoisting is the visible result of the creation phase.
What happens when code reads a variable that is not declared in any scope of the chain?
Reading an unresolvable name throws (typeof is the exception). Assigning to one creates a global in sloppy mode and throws in strict mode.
Function inner is defined inside function outer. Which statement is true?
Lookups go outwards only. outer would have to receive inner's data through a return value.
In an ES module, var version = "1.0"; is written at the top level. Is it a property of the global object?
Only classic scripts put top-level var and function declarations on the global object. Modules (and Node.js CommonJS files) keep them local.
Which statement best defines a closure?
Closures follow directly from lexical scope: the inner function's scope chain keeps the outer variables alive.
Timed online tests with instant scores and explanations.