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

Is that your question when you see var x = 10? What does the JavaScript var keyword do? Why is it used, sometimes, to declare JavaScript variables? The var keyword is your friend. Find out why.

If you are like a lot of people, you have seen the var keyword used in JavaScript code to declare variables. Additionally, you have seen plenty of code that does not use the var keyword. For example, both of these lines of code are legal declarations of a variable named x.

What gives? Even seasoned Web developers do not always know exactly how using, and not using, the var keyword affects variable declaration. The var keyword is used to declare (or create) variables, but because its use is not required, this inconsistency in how variables are declared leads to confusion. If it's not required, why use it? And what is the difference between using it and using not it?



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