JavaScript Closure Counter Function Challenge

Calling all JavaScript developers! I've got a tricky closure question that might just make you scratch your head. Can you solve this? function createCounter() { let count = 0; return function() { count++; console.log(count); }; } const counter1 = createCounter(); const counter2 = createCounter(); counter1(); counter1(); counter2(); What will be logged to the console, and why? 🤔 Share your answers and explanations below! Let's see who cracks it. 👇 #JavaScript #Closures #CodingChallenge #WebDevelopment

  • text

Bcz first counter1 invoked count =0 then 0+1 is print Again counter1 is invoked now count =1 and 1+1=2 print 2 And last counter2 invoked which count start from 0 so print 0+1 =1

See more comments

To view or add a comment, sign in

Explore content categories