Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BerwynIrish

Why does javascript require var declaration?

This topic is 5387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
i''m not sure bout the underworks behind javascript 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!