Javascript test ( object && object !== "null" && object !== "undefined" )

I don't think you can make that any simpler, but you could certainly refactor that logic into a function:

function isRealValue(obj)
{
 return obj && obj !== 'null' && obj !== 'undefined';
}

Then, at least your code becomes:

if (isRealValue(yourObject))
{
 doSomething();
}

If you have jQuery, you could use $.isEmptyObject().

$.isEmptyObject(null)
$.isEmptyObject(undefined)
var obj = {}
$.isEmptyObject(obj)

All these calls will return true. Hope it helps

Tags:

Javascript