PDA

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



XmisterIS
Feb 15th, 2012, 01: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.

DaveyErwin
Feb 15th, 2012, 03:07 PM
https://developer.mozilla.org/en/JavaScript/Strict_mode

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