Is it possible to increment the value of i
each time myFunction()
is called? Currently, i
is always initialized to 0
, resulting in zero as the output. How can we ensure that i
remains a local variable and gets properly incremented? Can this be achieved using closures?
function myFunction(){
var i = 0;
console.log(i);
i++
};
myFunction();
myFunction();
myFunction();
myFunction(); // Desired result: 3