There is one thing in JavaScript which I can't understand.
When we declare a variable like this:
var var_Name
it called Local Variable.
When we declare a variable like this:
var_Name
it called Global Variable.
When exactly the browser resets their values? Once the page refreshes?
What I mean is, when the pages loaded, I declare a variable, and by the time, I increases it's value. When the value is going to set back to it's original initialization? When I refresh the page? or if it's a Local Variables it refreshes right away after stopping using it and if it's Global variable it refreshes only when the page refreshes?
Thanks!
There is one thing in JavaScript which I can't understand.
When we declare a variable like this:
var var_Name
it called Local Variable.
When we declare a variable like this:
var_Name
it called Global Variable.
When exactly the browser resets their values? Once the page refreshes?
What I mean is, when the pages loaded, I declare a variable, and by the time, I increases it's value. When the value is going to set back to it's original initialization? When I refresh the page? or if it's a Local Variables it refreshes right away after stopping using it and if it's Global variable it refreshes only when the page refreshes?
Thanks!
Share Improve this question asked Mar 8, 2013 at 14:40 kfirbakfirba 5,47114 gold badges48 silver badges74 bronze badges 3- I believe page loading doesn't effect variabls – Jacob Commented Mar 8, 2013 at 14:45
- 1 @Jacob: that's incorrect. See answers below – Dancrumb Commented Mar 8, 2013 at 14:47
- 1 global just means its available to all files and functions, local is limited to where it is declared. variables just hold the data you give it, they do not act like sessions or persistent data, so they will be gone after reload, until given some data to hold for the new instance – ashley Commented Mar 8, 2013 at 14:48
4 Answers
Reset to default 3Global declaration:
var_Name = 1;
// is equal to
var var_Name = 1;
// and is also equal to
window.var_Name = 1;
The variable lives as long as the window object does. So it will be available until you leave/reload the current page.
A local variable inside a function lives as long as the function is running:
function y(){
var x = 1;
console.log(typeof x !== 'undefined');
}
y();
console.log(typeof x === 'undefined');
A global variable can be accessed from any function in any file loaded by the page. And when the page reloads the variable is unset until some code is run again and assign a value to it.
The Javascript engine does not maintain any variable values over a page loads. Thus, any global variables that were declared and defined during the life of a page will be lost when that page reloads.
Variables declared with var
are limited to the scope in which they are defined. For a browser, there is only Function scope and Global scope. As a result, a local variable declared in a function will cease to exist once that function returns, unless it is captured by a closure (a large topic that is worth searching the Internet about).
At no point to variables 'refresh' to their originally assigned values without an explicit action by the program. They are either in scope or out of scope and when in scope, they have whatever value was last assigned to them.
It's worth noting that executing the following outside of a function:
var foo = 1;
will still create a Global variable, since you're in the Global scope if you're not in a Function's scope.
Global variable is visible on all document. Local variable is visible only in block you define it. Variables will not be visible on the other documents.
When you refresh page all the variables are newly set that mean you won't get any variable's value until you save it (for example in cookie or via ajax).