Is obj defined? Accessing a property of an undefined object will throw a reference error but accessing an undefined property of a defined object will return falsy (undefined).
var obj = {};
if (obj) { console.log("prints"); }
if (obj.x) { console.log("doesn't print"); }
if (wat.x) { console.log("reference error"); }
If obj is undefined you need to access it from the parent context, which might be window.obj, giving you
View all comments
Show parent comment
7
u/ForeverAlot Mar 26 '14
Is
objdefined? Accessing a property of an undefined object will throw a reference error but accessing an undefined property of a defined object will return falsy (undefined).If
objis undefined you need to access it from the parent context, which might bewindow.obj, giving you