Why does javascript require var declaration?

Started by
1 comment, last by BerwynIrish 20 years, 6 months ago
This is something I''ve been curious about for a while. Why would a language require variable declaration when there''s only one type of variable? I don''t know anything about the theory of language design, so if this question is not easy to answer in simple terms, I''ll accept the answer that I don''t have the background to understand. I am versed in both high and low level languages, however, so I am familiar with the the benefits and drawbacks of strong vs weakly typed languages from a programming perspective. I''ve also made some assumptions about what goes on behind the scenes in interpreted languages, but the fact that an interpreter would benefit somehow from requiring the programmer to declare variables when all variables are of the same type anyway tells me my assumptions are off.
Advertisement
i''m not sure bout the underworks behind &#106avascript myself. but based on my observation, as noticed in how the var works in flash actionscript too, is that the ''var'' command plays a good role in variable scoping.

just using a variable, say ''var1'', within a function without the var command would probably imply referencing a global variable. by instantiating a var within a function''s local scope, there would be no ambiguity when referencing the variable.
- To learn, we share... Give some to take some -
Forcing you to declare your variables also makes it harder to accidently mispell your variable when using it. If you accidentally mistype a variable you''ve been using in a language which doesn''t require you to declare your variables, then you won''t get an error, whereas if you had to declare them you''re going to get an error (which is a good thing).

...unless of course you accidently typo and your typo happens to be the spelling of another variable

This topic is closed to new replies.

Advertisement