views:

467

answers:

5

In Javascript we don't have to declare a variable with 'var' keyword before using it. We can straight away do things like myCount = 12; or myStr = "Hello"; (where myCount, myStr are not declared before). Any such usage, declares and initializes the variables in the 'global' scope.

What could be the reasons for providing this feature? And is it a good standard at all?

UPDATE: My question is not what the differences are between 'using a variable without declaring' and 'declaring and then using' and how it affects scope.

My question is 'why it is allowed in javascript to use a variable directly without declaring it' as most of the programming languages have a strict check on this.

UPDATE : I see the following quoted text as a bad effect of this feature. So, why have this feature at all?

"Suppose there is a globally declared variable 'x' (var x="comparison string") already which i am unaware of and i with intention of creating my own global 'x' inside one of my functions initialize one(x=-1) and there i end up breaking other functionality.

So, is there any good reason at all? for having this feature?

+4  A: 

Good reasons? Honestly can't think of one, it's one of the few things I really dislike about JS.

It's possible because everything happens within the global scope if not otherwise controlled and JS allows implicit variable creation like this. The cost of this is enormous potential for scoping bugs and pollution, and only benefit given that "this" exists to explicitly define self scope and "window" and "document" exist for global referencing, is saving a few characters - which is no reason at all.

annakata
yes, exactly my point. suppose there is a globally declared variable 'x' already which i am unaware of and i with intention of creating my own global 'x' inside my function initialize one and there i end up breaking other functionality. is there any good reason at all?
Chandan .
As I said, it's beyond me if there is one. Better ask Brendan.
annakata
ok. kewl. I hope Brendan passes by this thread.
Chandan .
+2  A: 

My question is 'why it is allowed in javascript to use a variable directly without declaring it' as most of the programming languages have a strict check on this.

That's the difference between statically and dynamically typed languages. Javascript is dynamically typed, so there is no need to declare first. As it was pointed out in other answers, var keyword is more responsible for scope of a variable than its declaration.

And I don't think that most programming languages have a check on that.

SilentGhost
yeah, but, it causes problems than being of great help.
Chandan .
there are statically typed with automatic variables (haskell?) and dynamically typed without them (Scheme?)
Javier
@ChanLFC: for example?
SilentGhost
for example, please refer to second update in my question. you may call it programmer's ignorance but when a file can have multiple authors(maintainance projects which go for years) one can't blame the programmer who makes that mistake.
Chandan .
I'd say, proper variable naming is a good reason to refactor your code.
SilentGhost
+1  A: 

Because it is a scripting language. Fact kids. And that is how it was designed!

JohnnyB
+1  A: 

Lua has a similar issue: any non-declared variable is global by default. In the mailing list it's a recurring theme, asking why shouldn't it be 'local by default'. unfortunately, that would introduce very nasty ambiguities in the language, so the conclusion typically is "global by default is bad, local by default is worse"

Javier
well, why have anything by default in the first place? if it doesn't have a var keyword attached and isn't declared before then throw an error. simple :-)
Chandan .
+5  A: 

Javascript was intended for very simple scripts in the browser. Requiring variable declarations seemed unnecessary.

Of course, it's an error in the language. And the makers of javascript know that. They wanted to change it. But they couldn't. Why?

Because Microsoft had already reverse engineered JavaScript and created their duplicate JScript, with bugs and all. Microsoft vetoed any changes, even bugfixes, since they were adamant about not breaking anyones scripts. So even if they changed JavaScript, JScript in IE would stay the same.

It's not a good reason. But it's the one we have.

Source: I got my javascript history class from Douglas Crockford: "The JavaScript Programming Language", http://video.yahoo.com/watch/111593/1710507 This part of the story is between 9 and 11 minutes into the video.

Magnar
Embrace. Extend. Extinguish.
annakata
that is a sad reason indeed. an unwanted feature we see and use afterall.
Chandan .
i think yours is the answer. waiting to see if someone knows something else. till then one vote up for you.
Chandan .
Isn't it also a question of backwards compatibility with previous versions? Nevermind alternate implementations, which already have different APIs.
Mr. Shiny and New
Yes, backwards compatibility makes it hard to change now. But this change and several other bugfixes were proposed and would have been made, had it not been for Microsofts vetoes.
Magnar
Couldn't they add something to the language like the OPTION EXPLICIT in VBScript that forces declaration to be required? It would need to have some scoping rules so as to not interfere with Included files etc.
Kristen
or a type declaration like html, yeah that's aone way to solve it - not gonna happen now though
annakata
@annakata: ECMAScript 3.1 will include a 'strict mode'!
Christoph