Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Construct Closure Using Function Expression Syntax</title> </head> <body> <script> // Anonymous function expression var myCounter = (function() { var counter = 0; // Nested anonymous function return function() { counter += 1; return counter; } })(); document.write(myCounter() + "<br>"); // Prints: 1 document.write(myCounter()); // Prints: 2 </script> </body> </html>