JavaScript Hoisting & Temporal Dead Zone Quiz

JavaScript
0 Passed
0% acceptance

Tackle 40 questions covering hoisting concepts, var behavior, function declaration hoisting, function expressions, let/const characteristics, temporal dead zone pitfalls, runtime errors, and best practices.

40 Questions
~80 minutes
1

Question 1

What does hoisting describe in JavaScript?

A
The engine allocating memory for declarations during the creation phase before executing code
B
An optimization that inlines functions
C
Moving DOM elements to the top of the page
D
A CSS layout behavior
2

Question 2

What prints here?

javascript
console.log(status)
var status = 'ready'
A
undefined
B
ready
C
Throws ReferenceError
D
null
3

Question 3

Why do many developers describe hoisting as a metaphor rather than a literal movement of code?

A
Because the source stays in place, but the engine creates bindings ahead of time
B
Because the compiler physically reorders lines
C
Because hoisting only applies to HTML
D
Because hoisting requires runtime eval
4

Question 4

What is logged?

javascript
function report() {
  console.log(level)
  var level = 3
}
report()
A
undefined
B
3
C
Throws ReferenceError
D
level
5

Question 5

How does var hoisting interact with block statements?

A
var ignores block boundaries and hoists to the enclosing function or global scope
B
var is block scoped like let
C
var disappears if placed inside a block
D
var becomes const when inside a block
6

Question 6

What output occurs?

javascript
if (true) {
  var mode = 'debug'
}
console.log(mode)
A
debug
B
undefined
C
Throws ReferenceError
D
false
7

Question 7

Why is relying on implicit undefined from var hoisting risky?

A
It can hide bugs because reading before assignment silently returns undefined rather than throwing
B
It crashes the browser immediately
C
It disables strict mode
D
It converts numbers to strings
8

Question 8

What logs here?

javascript
greet()
function greet() {
  console.log('hello')
}
A
hello
B
undefined
C
Throws ReferenceError
D
TypeError: greet is not a function
9

Question 9

How does function declaration hoisting assist with mutual recursion?

A
Functions can reference each other regardless of source order because declarations exist during creation
B
It forces the engine to inline both functions
C
It disables recursion
D
It requires modules
10

Question 10

What prints?

javascript
console.log(typeof describe)
function describe() {
  return 'info'
}
A
function
B
undefined
C
Throws ReferenceError
D
object
11

Question 11

How do function expressions assigned to var behave when accessed before assignment?

A
They act like any var: the binding exists but holds undefined, so calling it throws TypeError
B
They hoist the function body just like declarations
C
They automatically convert to arrow functions
D
They run immediately
12

Question 12

What happens here?

javascript
run()
var run = function () {
  console.log('running')
}
A
TypeError: run is not a function
B
running
C
undefined
D
Throws ReferenceError
13

Question 13

Why do many style guides prefer function declarations for top-level helpers?

A
They hoist completely, making the order of helper definitions less critical and improving readability
B
They disallow recursion
C
They run faster by default
D
They cannot access closures
14

Question 14

How can you intentionally avoid hoisting effects when defining utilities?

A
Use block-scoped let/const with function expressions so they remain in the temporal dead zone until declaration
B
Wrap them in eval
C
Use global variables only
D
Declare them twice with var
15

Question 15

What does this code output?

javascript
{
  console.log(state)
  let state = 'ready'
}
A
ReferenceError
B
undefined
C
ready
D
null
16

Question 16

Which statement about let hoisting is accurate?

A
let declarations are hoisted to the top of their block but remain uninitialized until execution reaches them
B
let declarations are not hoisted at all
C
let declarations become global variables
D
let declarations throw SyntaxError during creation
17

Question 17

Why must const declarations be initialized at the point of declaration?

A
Because the engine requires the binding to receive its value as soon as it leaves the temporal dead zone
B
Because const values are hoisted as objects
C
Because const disables hoisting entirely
D
Because const always creates global properties
18

Question 18

What happens?

javascript
console.log(total)
const total = 10
A
ReferenceError
B
undefined
C
10
D
TypeError
19

Question 19

How does block scoping with let/const help avoid hoisting confusion?

A
It limits visibility to the block, preventing accidental early usage outside the intended region
B
It disables closures
C
It automatically renames variables
D
It turns all numbers into strings
20

Question 20

What does this snippet log?

javascript
let flag = true
if (flag) {
  console.log(value)
  let value = 5
}
A
ReferenceError
B
undefined
C
5
D
false
21

Question 21

What is the temporal dead zone (TDZ)?

A
The time between hoisting and actual declaration execution where let/const bindings exist but remain uninitialized
B
A runtime optimization for functions
C
A special browser API
D
A part of CSS animations
22

Question 22

What error occurs?

javascript
{
  console.log(choice)
  const choice = 'A'
}
A
ReferenceError
B
TypeError
C
undefined
D
A
23

Question 23

Why does the TDZ exist for let and const?

A
To catch mistakes where code tries to use a variable before its explicit initialization, improving predictability
B
To slow down execution intentionally
C
To remove block scoping
D
To enforce implicit globals
24

Question 24

What is the output?

javascript
let ready = false
function toggle() {
  if (!ready) {
    const ready = true
    console.log(ready)
  }
}
toggle()
console.log(ready)
A
true then false
B
ReferenceError
C
false then true
D
true twice
25

Question 25

How can TDZ awareness improve code reviews?

A
Reviewers can spot accesses that occur before declarations, preventing runtime ReferenceErrors in modern codebases
B
It automatically rewrites code
C
It enforces camelCase names
D
It eliminates the need for tests
26

Question 26

What happens when this script runs?

javascript
(function () {
  console.log(item)
  let item = 'value'
})()
A
ReferenceError inside the IIFE
B
undefined
C
value
D
TypeError
27

Question 27

Which statement best explains why TDZ errors are preferable to silent undefined values?

A
They fail fast, making it obvious when initialization order is wrong
B
They automatically fix the bug
C
They only occur in production
D
They turn into compile-time warnings
28

Question 28

How can TDZ errors occur in loops?

A
Using let declarations inside loop headers before the loop body executes may still be safe, but referencing those bindings outside the loop block before declaration causes ReferenceError
B
TDZ only applies to functions, not loops
C
for loops disable hoisting entirely
D
Loops automatically wrap in var
29

Question 29

How can bundlers unintentionally introduce TDZ issues?

A
By reordering import statements or wrapping modules in functions that change initialization order, exposing earlier accesses
B
By converting const into var automatically
C
By disabling strict mode
D
By inserting DOM nodes
30

Question 30

What is logged?

javascript
for (let i = 0; i < 2; i += 1) {
  console.log(marker)
  let marker = i
}
A
ReferenceError each iteration
B
0 then 1
C
undefined then undefined
D
2 then 2
31

Question 31

Which habit reduces hoisting surprises in modules?

A
Declare variables at the top of their scope and assign immediately so readers know initialization order
B
Scatter declarations randomly
C
Use global vars for everything
D
Avoid comments about scope
32

Question 32

How can destructuring lead to TDZ issues?

A
Destructuring uses let/const semantics, so referencing destructured bindings before the destructuring statement executes triggers the TDZ
B
Destructuring only uses var
C
Destructuring happens at compile time
D
Destructuring disables TDZ
33

Question 33

Why do many teams enforce eslint rules like no-use-before-define?

A
To catch potential hoisting and TDZ issues statically before runtime
B
To convert all code to TypeScript automatically
C
To shrink bundle sizes
D
To enforce snake_case
34

Question 34

What does this pattern log?

javascript
function init() {
  let ready
  setup()
  ready = true
  return ready
  function setup() {
    console.log(ready)
  }
}
console.log(init())
A
undefined then true
B
ReferenceError
C
true then true
D
false then true
35

Question 35

What best practice keeps TDZ errors out of async code?

A
Initialize variables before scheduling asynchronous work that uses them
B
Rely on implicit globals
C
Declare everything with var
D
Disable strict mode
36

Question 36

What result appears?

javascript
let config
function prepare() {
  config = { ready: true }
}
prepare()
console.log(config.ready)
A
true
B
ReferenceError
C
undefined
D
Throws TypeError
37

Question 37

How does keeping declarations near their first use help prevent TDZ confusion?

A
It visually confirms initialization order, reducing the chance of referencing variables during their TDZ
B
It makes code slower on purpose
C
It automatically converts let to const
D
It disables hoisting entirely
38

Question 38

Why should you avoid relying on implicit globals when writing modern code?

A
Implicit globals reintroduce var-style hoisting and make it harder to reason about TDZ-safe initialization
B
Implicit globals automatically convert to const
C
Implicit globals disable promises
D
Implicit globals speed up execution
39

Question 39

How can TypeScript or Flow assist with hoisting and TDZ issues?

A
Type checkers can flag potential undefined accesses when the control flow reveals usage before assignment
B
They remove the need for runtime errors entirely
C
They always compile let into var
D
They disable block scope
40

Question 40

What is a simple rule of thumb for avoiding TDZ runtime errors?

A
Declare and initialize let/const variables before any statement that references them, especially inside blocks or modules
B
Always declare variables at the end of the file
C
Use var exclusively
D
Disable strict mode so TDZ is ignored

QUIZZES IN JavaScript