View Full Version : How to make javascript variable declaration strict?

Feb 15th, 2012, 02:50 PM
I've just discovered (through trial and error) that if a variable is assigned a value without first being declared, it automatically has global scope, even if it is assigned inside a function.

This is a pain in the butt because if you forget the var keyword, then suddenly ... whoops! You've got yourself a global variable!

Is there any way that I can make javascript throw an exception if I fail to declare a variable before assigning a value to it?

It would make things much easier, rather than having to hunt through increasingly complex functions to find that pesky unintentionally global variable.

Feb 15th, 2012, 04:07 PM

Feb 15th, 2012, 04:48 PM
thanks! It's easy when you know how ...