Global Variables in HTML

With JavaScript, the global scope is the complete JavaScript environment. In HTML, the global scope is the window object: all global variables belong to the window object. The window object is supported by all browsers. It represents the browser's window. All global JavaScript objects, functions, and variables automatically become members of the window object. Global variables are properties of the window object. Global functions are methods of the window object.

  // code here can use window.carName
  function myFunction() {
      carName = "Volvo";
  }

HTML file: Displayed by browser:
<body>
<p>In HTML, all global variables will become window variables.</p>
<p id="demo"></p>
<script>
  myFunction();
   document.getElementById("demo").innerHTML =
   "I can display " + window.carName;
   function myFunction() {
   carName = "Volvo";
  }
</script>
</body>

In HTML, all global variables will become window variables.

Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions.
Oh boy, duh, this is all over my head... -- I need to find another tutorial to understand this stuff!

BackTable of ContentsNext