What is the output of the following code? function outer() { let count = 0; return function inner() { return ++count; }; } const a = outer(); const b = outer(); console.log(a(), a(), b());

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code? function outer() { let count = 0; return function inner() { return ++count; }; } const a = outer(); const b = outer(); console.log(a(), a(), b());

Key points

  • Each call to `outer()` creates a new closure with its own `count` variable
  • The `inner()` function increments the `count` variable each time it is called
  • `a()` and `b()` have separate `count` variables due to closures
  • The output is determined by the order of function calls

Ready to go further?

Related questions