How to Accurately Check Property Existence in JavaScript Objects
This article compares three common JavaScript techniques—truthy checks, the in operator, and hasOwnProperty()—explaining their behavior with various value types, prototype inheritance, and edge cases, and provides code examples to help you choose the most reliable method for property detection.
1. Truthy Check
A simple way to test whether a property holds a truthy value is to coerce it with !!. This approach treats values like 0, '', null, undefined, and NaN as false, which can lead to pitfalls.
const myObj = {
a: 1,
b: 'some string',
c: [0],
d: {a: 0},
e: undefined,
f: null,
g: '',
h: NaN,
i: {},
j: [],
deleted: 'value'
};
delete myObj.deleted;
console.log(!!myObj['a']); // 1, true
console.log(!!myObj['b']); // 'some string', true
console.log(!!myObj['c']); // [0], true
console.log(!!myObj['d']); // {a: 0}, true
console.log(!!myObj['e']); // undefined, false
console.log(!!myObj['f']); // null, false
console.log(!!myObj['g']); // '', false
console.log(!!myObj['h']); // NaN, false
console.log(!!myObj['i']); // {}, true
console.log(!!myObj['j']); // [], true
console.log(!!myObj['deleted']); // falseBecause falsey values are treated as absent, this method must be used with caution.
2. in Operator
The in operator returns true if a property exists anywhere in the object's own properties or its prototype chain, regardless of the property's value.
const myObj = {
someProperty: 'someValue',
someUndefinedProp: undefined,
deleted: 'value'
};
delete myObj.deleted;
console.log('someProperty' in myObj); // true
console.log('someUndefinedProp' in myObj); // true
console.log('toString' in myObj); // true (inherited)
console.log('deleted' in myObj); // falseThe in operator is not affected by falsey values, but it also reports properties found on the prototype chain. If you need to ignore inherited properties, use the next method.
3. hasOwnProperty()
hasOwnProperty()(inherited from Object.prototype) checks only the object's own properties, excluding the prototype chain.
const myObj = {
someProperty: 'someValue',
someUndefinedProp: undefined,
deleted: 'value'
};
delete myObj.deleted;
console.log(myObj.hasOwnProperty('someProperty')); // true
console.log(myObj.hasOwnProperty('someUndefinedProp')); // true
console.log(myObj.hasOwnProperty('toString')); // false
console.log(myObj.hasOwnProperty('deleted')); // falseNote that not every object inherits from Object. For objects created with Object.create(null), hasOwnProperty is undefined.
const cleanObj = Object.create(null);
cleanObj.someProp = 'someValue';
// TypeError: cleanObj.hasOwnProperty is not a function
console.log(cleanObj.hasOwnProperty('someProp'));In such rare cases you can call hasOwnProperty from Object.prototype directly:
const cleanObj = Object.create(null);
cleanObj.someProp = 'someValue';
console.log(({}).hasOwnProperty.call(cleanObj, 'someProp')); // trueConclusion
All three methods have appropriate use‑cases; understanding their differences lets you pick the most suitable one so your code behaves as expected.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
