1//Access Global variable
2var foo = "foobar";
3foo === window.foo; // Returns: true
4
5
6//Accessing Global Functions
7function greeting() {
8 console.log("Hi!");
9}
10
11window.greeting();
12
1var x = {name: "John"}; // This is a global object
2for(let i=0; i<10; i++){
3 // Variable x is alive here
4 // Variable i is alive here
5
6 let a = 3;
7 // Variable a is alive here
8
9 var y = 0;
10 // Variable y is alive here
11}
12// Variable i is dead here
13// Variable a is dead here
14// Variable x is alive here
15// Variable y is alive here
16console.log(x); // {name: "John"}
17console.log(y); // 0