What is the output of the following? “`js const fn = (function() { let count = 0; return { increment: () => ++count, getCount: () => count }; })(); fn.increment(); fn.increment(); console.log(fn.getCount()); “`

JavaScript Professional Medium

JavaScript Professional — Medium

What is the output of the following? “`js const fn = (function() { let count = 0; return { increment: () => ++count, getCount: () => count }; })(); fn.increment(); fn.increment(); console.log(fn.getCount()); “`

Key points

  • The IIFE pattern allows for private variables in JavaScript
  • The `increment` method uses a closure to access and modify the private `count` variable
  • The `getCount` method returns the current value of the `count` variable

Ready to go further?

Related questions