JavaScript Scope Types (Global, Block, Function) Quiz

JavaScript
0 Passed
0% acceptance

Learn 30 questions that analyze JavaScript global scope, function scope, block scope, var quirks, visibility rules, shadowing, scope chains, and best practices for managing identifiers.

30 Questions
~60 minutes
1

Question 1

What defines the global scope in a browser environment?

A
All identifiers attached to the window object
B
Identifiers declared inside a function
C
Identifiers declared inside a block
D
Identifiers imported via ES modules
2

Question 2

Why is polluting the global scope discouraged?

A
Global variables are shared across the entire runtime and can create naming collisions
B
Global variables cannot be read by functions
C
Global variables trigger syntax errors
D
Global variables automatically become constants
3

Question 3

What logs here?

javascript
var color = 'blue'
function show() {
  console.log(color)
}
show()
A
blue
B
undefined
C
Throws ReferenceError
D
null
4

Question 4

How can you reduce unintended global variables in a script?

A
Enable strict mode so undeclared assignments throw errors
B
Declare everything with var
C
Avoid using functions
D
Store values on document
5

Question 5

What does it mean that var is function-scoped?

A
Variables declared with var are confined to the function where they are declared
B
var creates block scope
C
var only works inside loops
D
var requires type annotations
6

Question 6

What prints?

javascript
function demo() {
  var value = 10
  if (true) {
    var value = 20
  }
  console.log(value)
}
demo()
A
20
B
10
C
undefined
D
Throws ReferenceError
7

Question 7

Why do nested functions have access to their parent function variables?

A
Because of lexical scoping, the inner function inherits the outer function’s environment
B
Because variables are copied to every function automatically
C
Because the outer function runs again before the inner function
D
Because the event loop injects variables dynamically
8

Question 8

What is block scope?

A
A region enclosed by braces where let/const declarations remain local
B
The same as the global scope
C
A scope created only by functions
D
A region created by HTML elements
9

Question 9

What logs?

javascript
if (true) {
  const label = 'inner'
  console.log(label)
}
console.log(typeof label)
A
inner then undefined
B
inner then inner
C
inner then string
D
inner then object
10

Question 10

Why are let and const preferred for block scope?

A
They limit visibility to the smallest necessary region and prevent accidental leaks
B
They become global automatically
C
They hoist to the top of the file
D
They only allow numbers
11

Question 11

Which statement about var inside blocks is true?

A
var ignores block boundaries and remains function-scoped
B
var is limited to the block where it appears
C
var becomes read-only in blocks
D
var creates a new scope per loop iteration automatically
12

Question 12

What prints?

javascript
for (var i = 0; i < 2; i += 1) {}
console.log(i)
A
2
B
undefined
C
Throws ReferenceError
D
NaN
13

Question 13

What problem can arise when using var in loops with asynchronous callbacks?

A
All callbacks share the same loop variable and observe its final value
B
Callbacks cannot access var at all
C
var causes callbacks to run twice
D
It disables promises
14

Question 14

How does JavaScript resolve identifier lookups?

A
It searches the current scope, then moves outward along the scope chain until it finds a match
B
It randomly picks a scope
C
It only checks the global scope
D
It evaluates based on call stack order
15

Question 15

What logs?

javascript
const x = 1
function outer() {
  const x = 2
  function inner() {
    console.log(x)
  }
  inner()
}
outer()
A
2
B
1
C
undefined
D
Throws ReferenceError
16

Question 16

What is the temporal dead zone?

A
The phase between entering a block and executing let/const declarations where the identifiers cannot be accessed yet
B
A period where var variables are inaccessible
C
A loop construct
D
A promise state
17

Question 17

What is variable shadowing?

A
Declaring a variable with the same name in an inner scope, hiding the outer binding
B
Storing values in localStorage
C
Declaring variables without names
D
Assigning null to every variable
18

Question 18

What prints?

javascript
const status = 'global'
function report() {
  const status = 'local'
  console.log(status)
}
report()
console.log(status)
A
local then global
B
global then local
C
local then local
D
Throws ReferenceError
19

Question 19

How can excessive shadowing hurt readability?

A
It forces readers to track multiple bindings with the same name across different scopes
B
It prevents modules from loading
C
It disables linting
D
It makes code run slower by default
20

Question 20

What is the scope chain?

A
A linked list of lexical environments consulted when resolving variables
B
A history of DOM events
C
A list of imported modules
D
A stack of promises
21

Question 21

What logs?

javascript
const level = 'global'
function a() {
  const level = 'function'
  return function b() {
    console.log(level)
  }
}
a()()
A
function
B
global
C
undefined
D
Throws ReferenceError
22

Question 22

When does the scope chain for a function get determined?

A
When the function is defined, based on its lexical position
B
When the function finishes executing
C
When the call stack is empty
D
When the function is garbage collected
23

Question 23

Which best practice keeps scope usage predictable?

A
Prefer const for values that never change, and let for reassignable values, minimizing var usage
B
Always assign to undeclared identifiers
C
Use only global variables
D
Disable strict mode
24

Question 24

What does this snippet output?

javascript
(() => {
  const config = { retries: 3 }
  console.log(config.retries)
})()
// console.log(config)
A
3 then ReferenceError (commented line would fail)
B
3 then 3
C
undefined then undefined
D
Throws ReferenceError inside the IIFE
25

Question 25

Why do linting rules often forbid no-global-assign or no-implicit-globals?

A
To prevent silent creation or mutation of globals, which complicates reasoning about scope
B
To stop modules from importing dependencies
C
To improve CSS loading
D
To enable synchronous fetch
26

Question 26

How does using modules help manage scope?

A
Modules provide file-level scope, so declarations stay local unless explicitly exported
B
Modules copy all globals automatically
C
Modules force every variable to be global
D
Modules disable block scope
27

Question 27

What prints?

javascript
let total = 5
{
  let total = 8
  console.log(total)
}
console.log(total)
A
8 then 5
B
5 then 8
C
8 then 8
D
Throws ReferenceError
28

Question 28

Why is it helpful to minimize scope size?

A
Smaller scopes reduce the number of places a variable can be changed, improving predictability
B
Smaller scopes make code execute twice as fast
C
Smaller scopes disable garbage collection
D
Smaller scopes automatically memoize values
29

Question 29

How does destructuring inside blocks aid scope management?

A
It lets you unpack values close to where they are needed, keeping bindings localized
B
It converts variables into globals
C
It forces synchronous code
D
It disables strict mode
30

Question 30

What habit keeps scope chains simpler during refactoring?

A
Use descriptive names and avoid reusing identifiers for different purposes in nested scopes
B
Use single-letter names everywhere
C
Never comment on scope decisions
D
Place all variables on the global object for easy access

QUIZZES IN JavaScript