JS Quiz: Hard: Lexical Scope and Closure Chain

What will be logged to the console when the script runs?

function createCounter(initialValue) {
  let count = initialValue;

  function increment() {
    count++;
    return count;
  }

  function decrement() {
    count--;
    return count;
  }

  return { increment, decrement };
}

const counter1 = createCounter(5);
const counter2 = createCounter(10);

counter1.increment();
counter2.decrement();
counter1.increment();

console.log(counter1.increment());
console.log(counter2.decrement());
  • 7\n9
  • 7\n8
  • 6\n9
  • 6\n8
0 voters