Hoisting is a fundamental mechanism in JavaScript where variable and function declarations are notionally moved to the top of their containing scope during the compilation phase, prior to code execution. This behavior is governed by the creation phase of execution contexts, where memory space is allocated for identifiers. While traditional var declarations are hoisted and initialized with undefined, modern ES6 block-scoped declarations (let and const) are hoisted uninitialized into the Temporal Dead Zone (TDZ). Furthermore, function declarations are fully hoisted with their bodies intact, whereas function expressions remain unhoisted. Mastering these execution mechanics is vital for predicting code output and clearing technical software engineering interviews.
JavaScript Hoisting MCQs
1 min read
Correct Answer: a) A mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase before code execution
Explanation:
Hoisting conceptually moves declarations to the top of their scope during compilation, allowing functions and variables to be referenced before they appear in the source code.
Correct Answer: a) They are hoisted to the top of their scope and automatically initialized with `undefined`.
Explanation:
Var declarations are hoisted and initialized with `undefined`, meaning accessing them before their declaration line returns `undefined` rather than throwing an error.
Correct Answer: a) It throws a ReferenceError because it resides in the Temporal Dead Zone (TDZ).
Explanation:
While `let` and `const` are hoisted, they are not initialized with a default value, creating a Temporal Dead Zone from the start of the block until the declaration is evaluated.
Correct Answer: a) The execution phase from the start of a block scope until a `let` or `const` variable is fully initialized, during which accessing it throws a ReferenceError
Explanation:
The TDZ ensures developers do not accidentally use block-scoped variables before their intended initialization point.
Correct Answer: a) They are fully hoisted along with their complete function body definition, allowing them to be invoked before their written line in code.
Explanation:
Function declarations are completely hoisted with their bodies, making them executable anywhere within their enclosing scope.
Correct Answer: a) Only the variable identifier is hoisted according to its declaration keyword (`var`, `let`, or `const`), while the function assignment itself is not hoisted.
Explanation:
Function expressions follow standard variable hoisting rules depending on whether they use `var`, `let`, or `const`.
Correct Answer: a) `undefined`
Explanation:
Due to hoisting, the `var a` declaration moves to the top and initializes with `undefined` before `console.log` executes.
Correct Answer: a) `ReferenceError`
Explanation:
Because `let` variables are in the Temporal Dead Zone until their declaration line, accessing `a` early throws a ReferenceError.
Correct Answer: a) Yes, they are hoisted to the top of their scope, but they remain uninitialized in the Temporal Dead Zone, throwing a ReferenceError if instantiated before declaration.
Explanation:
Class declarations behave similarly to `let` and `const` regarding hoisting and TDZ restrictions.
Correct Answer: a) The function declaration takes precedence and overwrites the variable declaration during hoisting, unless the variable is subsequently assigned a value.
Explanation:
Function declarations are processed first during hoisting, meaning a function declaration overrides a `var` declaration with the same name.
Correct Answer: a) Creation / Compilation phase
Explanation:
JavaScript engines parse the code and set up memory allocations for variable and function declarations during the creation phase before executing line-by-line.
Correct Answer: a) No, code lines remain physically where you wrote them; hoisting is a mental model representing how the engine allocates memory for declarations during compilation.
Explanation:
Code is not physically rearranged; rather, declarations are registered in the variable environment record during compilation.
Correct Answer: a) `var` declarations ignore block boundaries and are hoisted to the enclosing function or global scope.
Explanation:
Because `var` is function-scoped rather than block-scoped, it punches through block boundaries during hoisting.
Correct Answer: a) They are block-scoped, hoisting only to the top of their immediate enclosing block curly braces.
Explanation:
Block scoping confines `let` and `const` hoisting strictly to their enclosing block bounds.
Correct Answer: a) `hello`
Explanation:
Function declarations are fully hoisted with their bodies, allowing successful invocation before their written line.
Correct Answer: a) `TypeError: foo is not a function`
Explanation:
During hoisting, `var foo` becomes `undefined`. Calling `foo()` evaluates to `undefined()`, which throws a TypeError because `undefined` is not callable.
Correct Answer: a) `ReferenceError`
Explanation:
Because `foo` is declared with `const`, it resides in the TDZ until execution reaches the assignment line, throwing a ReferenceError when called early.
Correct Answer: a) They follow the exact same hoisting rules as the variable declaration keyword (`var`, `let`, or `const`) used to store them.
Explanation:
Arrow functions are function expressions; their hoisting behavior depends entirely on whether `var`, `let`, or `const` is used.
Correct Answer: a) Duplicate `var` declarations are ignored, and the existing value is preserved.
Explanation:
JavaScript permits multiple `var` declarations of the same name in the same scope without syntax errors.
Correct Answer: a) It throws a SyntaxError during parsing.
Explanation:
Redeclaring a `let` variable within the exact same scope is strictly forbidden and throws a SyntaxError.
Correct Answer: a) Function parameters are implicitly declared and initialized inside the function scope during creation, acting as local variables.
Explanation:
Parameters are scoped to the function body and initialized when the function is invoked.
Correct Answer: a) `undefined`
Explanation:
Because `var` is function-scoped, the inner `var x` hoists to the top of the enclosing function/global scope, shadowing outer `x` and initializing as `undefined` before `console.log`.
Correct Answer: a) `ReferenceError`
Explanation:
The inner block declares `let x`, creating a TDZ for that block scope. Accessing `x` before its declaration throws a ReferenceError instead of reading outer `x = 10`.
Correct Answer: a) No, strict mode does not change basic hoisting mechanics, though it enforces stricter error checking on undeclared assignments.
Explanation:
Hoisting is a core compilation mechanism unaffected by the strict mode directive.
Correct Answer: a) They are not hoisted because they are not declarations; they only become global properties when execution reaches that line.
Explanation:
Hoisting applies exclusively to declarations (`var`, `let`, `const`, `function`, `class`), not naked assignments.
Correct Answer: a) It maintains Environment Records that store identifier bindings created during hoisting phases.
Explanation:
Environment records map variable and function declarations during the creation phase of an execution context.
Correct Answer: a) The variable `f` is hoisted as `undefined` to the top of the function scope, but the function assignment occurs conditionally at runtime.
Explanation:
Only the `var` declaration is hoisted; the assignment runs only when the conditional block executes.
Correct Answer: a) Generator function declarations are fully hoisted with their bodies, just like standard function declarations.
Explanation:
Generator function declarations follow identical hoisting rules to standard functions.
Correct Answer: a) "undefined"
Explanation:
The `typeof` operator is safe to use on completely undeclared identifiers, returning "undefined" without throwing a ReferenceError.
Correct Answer: a) `ReferenceError`
Explanation:
Unlike undeclared identifiers, evaluating `typeof` on a `let` or `const` variable inside its Temporal Dead Zone throws a ReferenceError.
Correct Answer: a) Yes, async function declarations are fully hoisted along with their function bodies.
Explanation:
Async functions follow the exact same hoisting rules as standard function declarations.
Correct Answer: a) The declared variables follow the hoisting rules of their declaration keyword (`var`, `let`, or `const`), while the destructuring evaluation runs at execution time.
Explanation:
Destructuring uses standard `var`, `let`, or `const` keywords, adhering to their respective hoisting and TDZ behaviors.
Correct Answer: a) The identifiers follow the standard hoisting rules of their respective declaration keywords (`var`, `let`, or `const`).
Explanation:
Array destructuring relies on standard declaration keywords for hoisting behavior.
Correct Answer: a) Import declarations are hoisted to the top of the module file statically during compilation before any code execution.
Explanation:
ES6 import declarations are statically hoisted, ensuring imported bindings are available throughout the module.
Correct Answer: a) Export statements are hoisted along with their declarations to make module bindings accessible statically.
Explanation:
Module exports are processed during static analysis and hoisting phases.
Correct Answer: a) Legacy hoisting behaviors can cause the function declaration to hoist irregularly to the enclosing function or global scope, varying across engines.
Explanation:
In non-strict mode, block-level function declarations exhibit non-standard hoisting semantics, which strict mode resolves by restricting them to block scope.
Correct Answer: a) The function declaration is strictly block-scoped to the enclosing `if` block.
Explanation:
Strict mode standardizes block-level function declarations, confining them strictly to their enclosing block bounds.
Correct Answer: a) To provide predictable block scoping and eliminate the counterintuitive `undefined` initialization and scope leakage associated with `var` hoisting.
Explanation:
Block scoping and the TDZ prevent bugs caused by premature variable access and scope leakage.
Correct Answer: a) `undefined`
Explanation:
Inside function `test`, `var a` hoists to the top of the function scope and initializes with `undefined`.
Correct Answer: a) `ReferenceError`
Explanation:
The `let a` declaration creates a TDZ inside the function scope, causing a ReferenceError when accessed before initialization.
Correct Answer: a) No, `const` prevents variable reassignment after initial declaration.
Explanation:
Constants are read-only references once initialized.
Correct Answer: a) No, `const` prevents variable reassignment, but object properties remain fully mutable.
Explanation:
const locks the variable identifier reference, not the internal structural data of objects or arrays.
Correct Answer: a) The catch clause error identifier is block-scoped to the catch block and hoisted within it.
Explanation:
ES6 catch parameters have their own block scope and hoisting rules.
Correct Answer: a) The `var` declaration is redundant because the parameter already initializes that identifier name in the function scope.
Explanation:
Parameters already occupy the function scope; redeclaring them with `var` does nothing harmful, though redeclaring with `let` throws an error.
Correct Answer: a) It throws a SyntaxError because `let` cannot redeclare an existing parameter identifier in the same scope.
Explanation:
Function parameters and `let` declarations share the function body scope, prohibiting duplicate identifier declarations.
Correct Answer: a) It resolves the identifier from the current Lexical Environment's Environment Record, traversing up the scope chain if necessary.
Explanation:
Environment records store hoisted bindings for quick lookup during execution.
Correct Answer: a) `1`
Explanation:
Inside function `b`, function hoisting creates a local variable `a` (via `function a() {}`), shadowing global `a`. Reassigning `a = 10` modifies the local variable, leaving global `a` untouched at `1`.
Correct Answer: a) When an inner scope declares a variable with the same name as an outer scope variable, masking the outer variable within that inner region.
Explanation:
Shadowing occurs when inner declarations take precedence over outer ones during scope chain resolution.
Correct Answer: a) If a function declaration and a `var` share a name, the function declaration takes precedence during hoisting, making it callable immediately.
Explanation:
Function declarations override `var` declarations during hoisting, so the identifier points to the function initially.
Correct Answer: a) `[Function: x]` then `5`
Explanation:
During hoisting, `function x()` takes precedence over `var x`. The first log prints the function. Then `x = 5` reassigns the variable, so the second log prints `5`.
Correct Answer: a) Object methods are part of object literals and are not hoisted; they are evaluated at runtime when the object is created.
Explanation:
Object literals and their methods are runtime expressions, not declarations subject to hoisting.
Correct Answer: a) The variable identifier follows the hoisting rules of its declaration keyword (`var`, `let`, or `const`), while the class definition itself is not hoisted.
Explanation:
Class expressions behave like function expressions; only their container variable identifier follows hoisting rules.
Correct Answer: a) `ReferenceError`
Explanation:
Evaluating `typeof b` while `b` is in the Temporal Dead Zone throws a ReferenceError, preventing array creation.
Correct Answer: a) To allow mutually recursive functions to call each other regardless of their relative order in the source code file.
Explanation:
Function hoisting was originally designed to facilitate calling functions before their declaration lines, making recursive function organization flexible.
Correct Answer: a) It is hoisted and attached as a property of the global `window` object.
Explanation:
Global `var` declarations create properties on the global window object in browsers, whereas `let` and `const` do not.
Correct Answer: a) It is hoisted into the global lexical environment but does not become a property of the global `window` object.
Explanation:
Global `let` and `const` declarations avoid polluting global object properties.
Correct Answer: a) `undefined`
Explanation:
The `var a` declaration hoists and initializes with `undefined` before assignment occurs.
Correct Answer: a) `TypeError: myFunc is not a function`
Explanation:
Var hoisting sets `var myFunc = undefined`. Invoking `myFunc()` attempts to call `undefined()`, throwing a TypeError.
Correct Answer: a) Default parameters evaluate in their own intermediate scope, meaning parameters defined earlier can be referenced by later default parameters.
Explanation:
Default parameter scope behaves like an intermediate block scope between function parameters and body.
Correct Answer: a) Labels are identifiers used for loops and blocks and are not subject to variable hoisting.
Explanation:
Labels are control flow markers, not variable or function declarations.
Correct Answer: a) `1`
Explanation:
In non-strict mode, block-level function declarations hoist to the function scope, making `foo` accessible outside the block.
Correct Answer: a) `ReferenceError`
Explanation:
In strict mode, block-level function declarations are strictly block-scoped, throwing a ReferenceError when accessed outside the block.
Correct Answer: a) No, hoisting applies exclusively to identifier declarations (`var`, `let`, `const`, `function`, `class`), not object or array properties.
Explanation:
Property access and mutations occur strictly at runtime.
Correct Answer: a) The variable is hoisted once to the outer function scope, sharing a single binding across all loop iterations.
Explanation:
Function-scoped `var` in loops shares one binding, which frequently causes closure bugs in asynchronous loops.
Correct Answer: a) A fresh variable binding is created for each individual iteration loop cycle, preventing closure capture bugs.
Explanation:
Using `let` in loop heads creates per-iteration bindings, correctly preserving loop index values in callbacks.
Correct Answer: a) `undefined`
Explanation:
Local `var x` hoists to the top of function `test`, shadowing global `x = 1` and initializing as `undefined` before logging.
Correct Answer: a) `ReferenceError`
Explanation:
Local `let x` creates a TDZ inside function `test`, throwing a ReferenceError when accessed before initialization instead of reading outer `x = 1`.
Correct Answer: a) Only the `var` identifier is hoisted as `undefined`; the function expression assignment occurs at runtime when execution reaches that line.
Explanation:
Function expression assignments never hoist their values, only their container variable identifiers.
Correct Answer: a) `undefined`
Explanation:
Var hoisting initializes `foo` as `undefined` during compilation.
Correct Answer: a) `ReferenceError`
Explanation:
Let hoisting leaves `foo` uninitialized in the TDZ, throwing a ReferenceError.
Correct Answer: a) `ReferenceError`
Explanation:
Const hoisting leaves `foo` uninitialized in the TDZ, throwing a ReferenceError.
Correct Answer: a) `TypeError: bar is not a function`
Explanation:
Var hoisting initializes `bar` as `undefined`. Calling `bar()` evaluates `undefined()`, throwing a TypeError.
Correct Answer: a) Nested function declarations are fully hoisted to the top of their enclosing outer function scope.
Explanation:
Function declarations hoist to the top of their immediate function or global container scope.
Correct Answer: a) It is accessible because `var` is function-scoped and ignores block boundaries.
Explanation:
Var declarations ignore try/catch block boundaries.
Correct Answer: a) It throws a ReferenceError because `let` is strictly block-scoped to the `try` block.
Explanation:
Let variables declared in a try block cannot be accessed outside that block.
Correct Answer: a) No, declaring a `const` variable without initialization throws a SyntaxError during parsing.
Explanation:
Constants require immediate initialization at declaration time.
Correct Answer: a) `undefined`
Explanation:
The IIFE declares local `var x`, which hoists to the top of the IIFE scope and initializes as `undefined`, shadowing global `x = 5`.
Correct Answer: a) `ReferenceError`
Explanation:
The IIFE declares local `let x`, creating a TDZ for that function scope and throwing a ReferenceError when accessed early.
Correct Answer: a) The later function declaration overwrites the earlier function declaration during hoisting.
Explanation:
When duplicate function declarations occur in the same scope, the subsequent declaration overwrites the previous one during hoisting.
Correct Answer: a) `2`
Explanation:
The second `foo` definition overwrites the first during hoisting, so invoking `foo()` prints `2`.
Correct Answer: a) Yes, every independent JavaScript execution context (including Web Workers) performs hoisting during its compilation phase.
Explanation:
All JS execution contexts follow the same compilation and hoisting principles.
Correct Answer: a) `undefined`
Explanation:
This is a named function expression, not a function declaration. Only the `var a` identifier hoists, initializing as `undefined`.
Correct Answer: a) `ReferenceError`
Explanation:
Const declarations reside in the TDZ, throwing a ReferenceError when accessed before initialization.
Correct Answer: a) `ReferenceError`
Explanation:
Let declarations reside in the TDZ, throwing a ReferenceError when accessed before initialization.
Correct Answer: a) `undefined` then `10`
Explanation:
Var ignores block scope, hoisting to the outer/global scope. First log prints `undefined`, assignment runs, second log prints `10`.
Correct Answer: a) `ReferenceError`
Explanation:
The first `console.log(a)` attempts to read `a` in outer scope where it does not exist, throwing a ReferenceError.
Correct Answer: a) Only their variable container identifier hoists according to its declaration keyword (`var`, `let`, or `const`), while the generator expression assignment runs at runtime.
Explanation:
Generator expressions follow standard variable expression hoisting rules.
Correct Answer: a) `ReferenceError` (because default parameter `x = x` attempts to reference `x` while it is in its own Temporal Dead Zone)
Explanation:
Default parameters evaluate in their own scope where parameters act like `let` declarations, meaning referencing `x` before initialization triggers a TDZ ReferenceError.
Correct Answer: a) It helps prevent subtle bugs, avoids unexpected `undefined` variable values, clarifies scope boundaries, and aids in debugging technical interview questions.
Explanation:
Mastering hoisting provides deep insight into JavaScript execution contexts, compilation phases, and variable scope resolution.
Related Posts
New
New
New

Top Python Fundamentals MCQs & Answers for Beginners
Python is a dynamically typed, high-level programming language created by Guido van Rossum in 1991. Renowned for its clear syntax…
August 27, 2026By MCQs Generator

Python Arrays MCQs
Unlike many other programming languages, Python does not have a built-in static array data structure in its core syntax, instead…
August 27, 2026By MCQs Generator

JavaScript ES6 Features MCQs
ECMAScript 2015 (commonly known as ES6) marked a massive evolutionary leap for JavaScript, introducing syntax enhancements that made code cleaner,…
August 29, 2026By MCQs Generator
Related Categories
New












AI & Data Science MCQ
5 topics
By MCQs Generator
New
Arts & Humanities MCQ
4 topics
By MCQs Generator
New
Civil Engineering MCQ
4 topics
By MCQs Generator
New
Commerce & Business MCQ
4 topics
By MCQs Generator
New
Competitive Exams MCQ
5 topics
By MCQs Generator
New
Electrical & Electronics Engineering MCQ
3 topics
By MCQs Generator
New
General Knowledge MCQ
2 topics
By MCQs Generator
New
General Science MCQ
4 topics
By MCQs Generator
New
Law & Judiciary MCQ
3 topics
By MCQs Generator
New
Mechanical Engineering MCQ
4 topics
By MCQs Generator
New
Medical & Health Sciences MCQ
4 topics
By MCQs Generator
New
Modern Tech Fields MCQ
3 topics
By MCQs Generator