0
0
Javascriptprogramming~20 mins

Practical closure use cases in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Closure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Closure and private variables
What is the output of this code when calling counter.increment() three times and then counter.get()?
Javascript
function createCounter() {
  let count = 0;
  return {
    increment() {
      count++;
    },
    get() {
      return count;
    }
  };
}
const counter = createCounter();
counter.increment();
counter.increment();
counter.increment();
console.log(counter.get());
A0
BTypeError
Cundefined
D3
Attempts:
2 left
πŸ’‘ Hint
Think about how the variable count is kept inside the function and accessed only through returned methods.
❓ Predict Output
intermediate
2:00remaining
Closure capturing loop variable
What will be logged by the following code?
Javascript
function createFunctions() {
  const funcs = [];
  for (var i = 0; i < 3; i++) {
    funcs.push(function() {
      return i;
    });
  }
  return funcs;
}
const functions = createFunctions();
console.log(functions[0]());
console.log(functions[1]());
console.log(functions[2]());
A3 3 3
B0 1 2
Cundefined undefined undefined
D0 0 0
Attempts:
2 left
πŸ’‘ Hint
Remember that var is function-scoped, not block-scoped.
❓ Predict Output
advanced
2:00remaining
Fixing closure in loop with IIFE
What is the output of this fixed code?
Javascript
function createFunctions() {
  const funcs = [];
  for (var i = 0; i < 3; i++) {
    (function(j) {
      funcs.push(function() {
        return j;
      });
    })(i);
  }
  return funcs;
}
const functions = createFunctions();
console.log(functions[0]());
console.log(functions[1]());
console.log(functions[2]());
Aundefined undefined undefined
B3 3 3
C0 1 2
DTypeError
Attempts:
2 left
πŸ’‘ Hint
The IIFE captures the current value of i as j for each function.
🧠 Conceptual
advanced
1:30remaining
Why use closures for data privacy?
Which of the following best explains why closures are used to create private data in JavaScript?
AClosures allow functions to access variables from their outer scope even after the outer function has finished executing.
BClosures prevent any function from accessing variables outside its own scope.
CClosures automatically encrypt variables to keep them private.
DClosures make variables global so all functions can share them.
Attempts:
2 left
πŸ’‘ Hint
Think about how inner functions remember variables from where they were created.
❓ Predict Output
expert
2:00remaining
Closure with asynchronous callbacks
What will be the output of this code snippet?
Javascript
function delayedLogger() {
  for (let i = 0; i < 3; i++) {
    setTimeout(() => {
      console.log(i);
    }, 100);
  }
}
delayedLogger();
Aundefined undefined undefined
B0 1 2
C3 3 3
D0 0 0
Attempts:
2 left
πŸ’‘ Hint
Consider how let differs from var in loops and closures.