Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Manipulating Global Variables</title> </head> <body> <script> // Global variable var counter = 0; // A function dedicated to manipulate the 'counter' variable function makeCounter() { return counter += 1; } // Calling the function makeCounter(); document.write(counter + "<br>"); // Prints: 1 makeCounter(); document.write(counter + "<br>"); // Prints: 2 // Trying to manipulate the 'counter' variable from outside counter = 10; document.write(counter); // Prints: 10 </script> </body> </html>