Global JavaScript Scope

A variable declared outside a function, becomes GLOBAL. A global variable has global scope: All scripts and functions on a web page can access it.

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

HTML file: Displayed by browser:
<body>
<p>A GLOBAL variable can be accessed from any script or function.</p>
<p id="demo">First Display Here</p>
<p id="demo1">Second Display Here</p>
<script>
  var carName = "Volvo";
   document.getElementById("demo").innerHTML =
   "I can display " + carName + " (1st Highlight)";

  myFunction();
   function myFunction() {
   document.getElementById("demo1").innerHTML =
   "I can display " + carName+ " (2nd Highlight)";

  }
</script>
</body>

A GLOBAL variable can be accessed from any script or function.

First Display Here

Second Display Here

I think I get it.

BackTable of ContentsNext