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

Conclusion

Basically, there are two reasons to use var:

  • Your code will be clearer. When you use var, you intentions are clear. If you are using it, you are declaring a new variable that is scoped to the current execution context (local inside a function, or global). And if in the same code there are places where you do not use var, it is clear that you are not declaring a new variable but overriding a previoulsy declared variable.
  • You code will be less buggy. Using var for every variable declaration guarantees that the variable is scoped to the correct execution context. So if you happen to have variables with the same name, they will not overwrite each other. And it will force you to create all global variables outside of functions, which is a good coding practice. All global variables should be declared at the top of your program before any functions.


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