Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Closures</title> </head> <body> <script> function makeCounter() { var counter = 0; // Nested function function make() { counter += 1; return counter; } return make; } /* Execute the makeCounter() function and store the returned value in the myCounter variable */ var myCounter = makeCounter(); document.write(myCounter() + "<br>"); // Prints: 1 document.write(myCounter()); // Prints: 2 </script> </body> </html>