I'm iterating through an array of simple (JSON-style) objects, some of which have a given property, and some that don't. If they have that property, it's safe to assume that it will never evaluate to a falsy value.
var objs = [
{'a': 1, 'b': 2, 'c': 3},
{'a': 4, 'b': 5},
{'a': 7, 'b': 8, 'c': 9}
];
My rather large application is triggering some (very unhelpful) errors in Internet Explorer 8, so I just want to cross this off the list of potential issues. Is it safe to use:
for (var i = 0; i < objs.length; ++i) {
if (objs[i].c) {
// objs[i].c is defined
}
}
I've used this sort of check many times in the application code, but do I need to be using obj.hasOwnProperty('c')
or typeof obj.c != 'undefined'
?
Thanks.