JavaScript Scope Types (Global, Block, Function) Quiz
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.
Question 1
What defines the global scope in a browser environment?
Question 2
Why is polluting the global scope discouraged?
Question 3
What logs here?
var color = 'blue'
function show() {
console.log(color)
}
show()Question 4
How can you reduce unintended global variables in a script?
Question 5
What does it mean that var is function-scoped?
Question 6
What prints?
function demo() {
var value = 10
if (true) {
var value = 20
}
console.log(value)
}
demo()Question 7
Why do nested functions have access to their parent function variables?
Question 8
What is block scope?
Question 9
What logs?
if (true) {
const label = 'inner'
console.log(label)
}
console.log(typeof label)Question 10
Why are let and const preferred for block scope?
Question 11
Which statement about var inside blocks is true?
Question 12
What prints?
for (var i = 0; i < 2; i += 1) {}
console.log(i)Question 13
What problem can arise when using var in loops with asynchronous callbacks?
Question 14
How does JavaScript resolve identifier lookups?
Question 15
What logs?
const x = 1
function outer() {
const x = 2
function inner() {
console.log(x)
}
inner()
}
outer()Question 16
What is the temporal dead zone?
Question 17
What is variable shadowing?
Question 18
What prints?
const status = 'global'
function report() {
const status = 'local'
console.log(status)
}
report()
console.log(status)Question 19
How can excessive shadowing hurt readability?
Question 20
What is the scope chain?
Question 21
What logs?
const level = 'global'
function a() {
const level = 'function'
return function b() {
console.log(level)
}
}
a()()Question 22
When does the scope chain for a function get determined?
Question 23
Which best practice keeps scope usage predictable?
Question 24
What does this snippet output?
(() => {
const config = { retries: 3 }
console.log(config.retries)
})()
// console.log(config)Question 25
Why do linting rules often forbid no-global-assign or no-implicit-globals?
Question 26
How does using modules help manage scope?
Question 27
What prints?
let total = 5
{
let total = 8
console.log(total)
}
console.log(total)Question 28
Why is it helpful to minimize scope size?
Question 29
How does destructuring inside blocks aid scope management?
Question 30
What habit keeps scope chains simpler during refactoring?
