Detecting Object Properties in JavaScript: Truthy, in Operator, hasOwnProperty
This article compares three common JavaScript techniques—truthy value checks, the in operator, and hasOwnProperty()—explaining how each works, their handling of falsy values and prototype inheritance, and providing code examples and edge‑case solutions for reliable property detection.
In JavaScript there are several ways to check whether an object has a particular property. Choosing the appropriate method depends on the specific requirements, so it is important to understand how each approach works.
1. Truthy check
A simple method is to coerce the property value to a boolean using !!.
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']); // true
console.log(!!myObj['b']); // true
console.log(!!myObj['c']); // true
console.log(!!myObj['d']); // true
console.log(!!myObj['e']); // false
console.log(!!myObj['f']); // false
console.log(!!myObj['g']); // false
console.log(!!myObj['h']); // false
console.log(!!myObj['i']); // true
console.log(!!myObj['j']); // true
console.log(!!myObj['deleted']); // falseAs shown, falsy values such as undefined, null, '', and NaN evaluate to false, so this method must be used with caution.
2. in operator
The in operator returns true if a property exists in the object or anywhere in its prototype chain.
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 falsy values, but it also reports properties inherited from the prototype chain, which may be undesirable in some cases.
3. hasOwnProperty()
hasOwnProperty()(inherited from Object.prototype) checks only the object's own properties, ignoring 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.prototype. For objects created with Object.create(null), hasOwnProperty is unavailable:
const cleanObj = Object.create(null);
cleanObj.someProp = 'someValue';
// TypeError: cleanObj.hasOwnProperty is not a functionIn such rare cases you can call the method from Object.prototype explicitly:
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 allows you to choose the most suitable one so that 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.
