JavaScript Closures & Lexical Environment Quiz
Study 40 JavaScript questions on lexical scoping, scope chains, closure creation, accessing outer variables, state preservation, memory considerations, and avoiding accidental closures.
Question 1
What does lexical scoping determine in JavaScript?
Question 2
Why is JavaScript described as having static scope?
Question 3
What does this code log?
const label = 'outer'
function print() {
console.log(label)
}
print()Question 4
Which statement best summarizes lexical scoping?
Question 5
What is the scope chain?
Question 6
What logs here?
const outer = 10
function first() {
const inner = 20
function second() {
console.log(outer + inner)
}
second()
}
first()Question 7
How does JavaScript search for a variable during execution?
Question 8
What is a lexical environment record?
Question 9
How do inner functions differ from outer functions regarding captured variables?
Question 10
What prints from this snippet?
function outer() {
const secret = 42
function reveal() {
return secret
}
return reveal
}
const fn = outer()
console.log(fn())Question 11
Why can outer functions not access variables declared inside inner functions?
Question 12
What does the term "closure" describe?
Question 13
When does JavaScript create a closure for a nested function?
Question 14
What’s logged here?
function createCounter() {
let count = 0
return () => ++count
}
const inc = createCounter()
console.log(inc())
console.log(inc())Question 15
Why do closures capture references rather than copies of variables?
Question 16
Which of the following is a sign that a closure was created?
Question 17
What prints?
function setup() {
const prefix = 'ID-'
return function make(id) {
return prefix + id
}
}
const buildId = setup()
console.log(buildId(7))Question 18
How can closures access block-scoped variables?
Question 19
What happens in this scenario?
function buildFormatter(locale) {
return function format(value) {
return new Intl.NumberFormat(locale).format(value)
}
}
const formatUS = buildFormatter('en-US')
console.log(formatUS(1234.56))Question 20
Why do closures enable partial application with ease?
Question 21
Which example demonstrates accessing outer variables?
Question 22
What is a common reason to use closures for state?
Question 23
What logs?
function makeToggle() {
let isOn = false
return () => {
isOn = !isOn
return isOn
}
}
const toggle = makeToggle()
console.log(toggle())
console.log(toggle())Question 24
How do closures help simulate private members?
Question 25
What does this snippet return?
function createBankAccount(initialBalance) {
let balance = initialBalance
return {
deposit(amount) {
balance += amount
return balance
},
withdraw(amount) {
balance -= amount
return balance
}
}
}
const account = createBankAccount(100)
console.log(account.withdraw(40))Question 26
Which pattern frequently relies on closures?
Question 27
What is logged?
const add = a => b => a + b
const addFive = add(5)
console.log(addFive(3))Question 28
Why are closures useful in event handlers?
Question 29
What does this code print?
function once(fn) {
let called = false
let result
return function (...args) {
if (!called) {
result = fn.apply(this, args)
called = true
}
return result
}
}
const logOnce = once((msg) => msg.toUpperCase())
console.log(logOnce('hello'))
console.log(logOnce('again'))Question 30
How do closures enable module-like patterns?
Question 31
How can closures impact memory usage?
Question 32
What logs?
function heavy() {
const largeArray = new Array(1000).fill(0)
return () => largeArray.length
}
const getLength = heavy()
console.log(getLength())Question 33
How can you avoid holding onto unnecessary data inside closures?
Question 34
Why should long-lived closures be profiled?
Question 35
What is a sign you might be creating accidental closures during loops?
Question 36
What is one way to avoid accidental loop closures capturing a single binding?
Question 37
What prints from this loop?
const funcs = []
for (let i = 0; i < 3; i += 1) {
funcs.push(() => i)
}
console.log(funcs[0](), funcs[2]())Question 38
Which scenario often indicates an accidental closure?
Question 39
How can factory functions help avoid closure-related bugs?
Question 40
What is a best practice when using closures heavily?
