var name = prompt(“Enter your name”, “Name”);
document.write(“<H2> Hello ” + name + “</H2>”);
In this example, the name is defined as global and accessed through different portions of the page. Now let us understand how it is saved inside the memory.
when it is running it takes the input from the user and stores that inside of the “name” variable. This is outside of any function hence it is placed inside the global scope.
Now when we require the “name” it checks the scope. It is not inside of any function or block so it then goes for the global scope. If found inside the global scope, the reference is established.
Let us take a second example
here c is defined inside of a function called product so this is a property of the function. All private property of any function is written inside of a structure of that function.
For further reference: