Javascript Detect Null Object

There are many methods to check if an object is null in javascript, this post gives most of the options that can used to detect this.

Well, first of all, in JavaScript null is an object. There’s anothervalue for things that don’t exist, undefined. The DOM returns null foralmost all cases where it fails to find some structure in thedocument, but in JavaScript itself undefined is the value used.

Second, no, they are not directly equivalent.If you really want to check for null, do:

if (null == yourvar) // with casting
if (null === yourvar) // without casting

If you want to check if a variable exist

if (typeof yourvar != ‘undefined’) // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers

If you know the variable exists but don’t know if there’s any valuestored 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 hasbeen assigned a value or not:

if (’membername’ in object) // With inheritance
if (object.hasOwnProperty(’membername’)) // Without inheritance

If you want to to know whether a variable autocasts to true: