Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
docs:programming:javascript:var [2007/11/11 13:53] – created billh | docs:programming:javascript:var [2008/08/03 00:25] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 24: | Line 24: | ||
for(var i in o) document.write(i, | for(var i in o) document.write(i, | ||
</ | </ | ||
+ | |||
+ | ===== Global, Local, Implicit and Scope ===== | ||
+ | If you attempt to read the value of an undeclared variable, JavaScript generates an error. | ||
+ | |||
+ | Global variables are accessible everywhere, whereas local variables are only accessible within the current function scope. | ||
+ | |||
+ | A local variable takes precedence over a global variable of the same name. Be careful and understand that you can hide global variables within a function if using var on a variable of the same name as an existing global variable, and be extra careful because you can overwrite a global variable value if you decide not to use var. The best way to avoid problems is simple: | ||
+ | |||
+ | ===== Notes ===== | ||
+ | * Variables declared with var are // | ||
+ | * It is legal and harmless to declare a variable more than once with the var statement. |