How do you check for an undefined variable in javascript?
In JavaScript null was an object. There ' s another value for things that don ' t exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, and in JavaScript itself Undefined is the value used.
Second, no, they is not directly equivalent. If you really want to check for null, do:
if (null = = Yourvar)//with castingif (null = = = Yourvar)//Without casting
If you want to check if a variable exist
if (typeof yourvar! = ' undefined ')//any Scopeif (window[' varname ')! = undefined)//Global Scopeif (window[' varname ')! = void 0)//old browsers
If you know the variable exists but don ' t know if there ' s any value stored in it:
if (undefined! = Yourvar) if (void 0! = Yourvar)//For older browsers
If you want to know if a member exists independent of whether it had been assigned a value or not:
if (' MemberName ' in object)//With Inheritanceif (Object.hasownproperty (' membername '))//Without inheritance
If you want to know whether a variable autocasts to true:
if (variablename)
Source
Original: Http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript
How to check a isn't defined variable in JavaScript