Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Closures</h1> <p>Counting with a local variable.</p> <p id="demo">0</p> <button type="button" onclick="myFunction()">Count!</button> <script> function myCounter() { let counter = 0; return function() { counter++; return counter; }; } const add = myCounter(); function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> </html>