Possible Duplicate:
Difference between using var and not using var in JavaScript
Hi All
Declaringvar myVar; with var keyword or declaring myVar without it.
This might be a stupid question for some, but I’m in a learning process with javascript.
Sometime, when reading other people’s code, I see that some variables are declared without the ‘var’ at the front. Just to emphasise, these variables are not even declared as parameters of a function.
So what’s the difference between declaring a variable with or without the ‘var’ keyword?
Many Thanks
If you don’t use it, it might be a global variable, or in IE’s case, it’ll just blow up.
There are cases you don’t need it, for example if it’s a parameter to the function you’re in, it’s already declared. Always use
varin any other cases (other cases being: unless you’re manipulating a variable that already exists). No matter what scope it needs to be defined at, explicitly define it there.