DHTMLdev.com — Dedicated to quality Web development articles and tutorials
To var or Not to var PDF Print E-mail
Sunday, 19 March 2006
Article Index
To var or Not to var
What Does var Do?
It Is All About Context
How var Affects Variable Scope
Why You Should Always Use var
Conclusion

How var Affects Variable Scope

We're almost done. Just one more piece to the puzzle.

Just as declaring a variable in a execution context (inside a function, so it is local, or outside all functions so it is global), the var keyword can also affect whether a variable is local or global.

If you use the var keyword, you are writing a var statement, such as var x = 10. Using a var statement declares that the variable should be local to the current execution context. Not using a var statement, however, declares that the variable should be global. So if you don't use var, no matter where you declare the varible, it will have a global scope.

Here's an example.



Last Updated ( Sunday, 19 March 2006 )
 
< Prev   Next >