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 Scope</h1> <h3>Counting with a global variable.</h3> <p id="demo"></p> <script> // Initiate counter let counter = 0; // Function to increment counter function add() { counter += 1; } // Call add() 3 times add(); add(); add(); // The counter should now be 3 document.getElementById("demo").innerHTML = "The counter is: " + counter; </script> </body> </html>