var
. AVOID THESE. We will not discuss them further.let
and const
(for constant variables).Global variables are really nothing more than properties of the global object:
a = 2; // Defining a global variable
window.a; // That variable exists as part of the global object
window.b = 4; // Can define global variables as properties of the global object
b; // Returns 4
delete window.b;
Note: Very few things in Javascript are protected. For instance this line overwrites the Math object:
Math = {} Math.sin(2); /// An error now
let
or const
.Here is an example of what can go horribly wrong if you are not careful: local_global.html and local_global.js
Note: Files loaded via
<script>
tags all share the same global space. Whatever you do in one file can impact the other files.