Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I check whether an object has its own property?

Quick answer

Use Object.hasOwn(object, key) to check whether a property belongs directly to that object. Use key in object when you want to include properties inherited through its prototype chain. Both checks can return true for an own property, but only in returns true for an inherited one. These checks test presence, not whether the property's value is useful or non-undefined. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have an object and want to know whether it has a particular property itself, rather than receiving that property from its prototype. Which JavaScript check should I use?

Geez

Use Object.hasOwn(object, key). It returns true for a property directly on the object and false for one found only through the prototype chain. By contrast, key in object checks both the object and its prototype chain. [S1] [S2]

Here is a small example with one own property and one inherited property:

const proto = { inherited: 2 };
const item = { own: 1 };
Object.setPrototypeOf(item, proto);

console.log(Object.hasOwn(item, "own"));       // true
console.log(Object.hasOwn(item, "inherited")); // false
console.log("own" in item);                    // true
console.log("inherited" in item);              // true

The choice depends on what “has” means for your task: direct ownership, or availability anywhere along the prototype chain.

Follow-up question

Can you lay out the results clearly? I want to avoid mixing up which check includes inherited properties.

Geez

For the item example above, the results are:

  • Object.hasOwn(item, "own") → true
  • "own" in item → true
  • Object.hasOwn(item, "inherited") → false
  • "inherited" in item → true

Object.hasOwn() asks whether the property is directly on item. The in operator asks whether it is on item or anywhere in its prototype chain. [S1] [S2]

So if you are filtering an object’s directly defined fields, use Object.hasOwn(). If inherited properties should count as present, use in. The checks do not tell you the property's value; they answer whether the property is present under their respective rules.

Follow-up question

What if the property exists but its value is undefined? And what if I only care whether I can access a name?

Geez

A property can be present even when its value is undefined. The in operator still returns true in that case; presence is different from checking whether a value is defined. [S2] Object.hasOwn() likewise checks whether the property is directly on the object, including when its value is null or undefined. [S1]

For example:

const item = { note: undefined };

Object.hasOwn(item, "note"); // true: own property
"note" in item;              // true: property is present
item.note !== undefined;     // false: value is not non-undefined

If your requirement is specifically “does this object directly define note?”, use Object.hasOwn(). If it is “is note present on this object or its prototype chain?”, use in. If your requirement is about the value, check that value separately.

Follow-up question

Can you give me a reusable snippet and a quick way to choose between the two checks?

Geez

Use this small helper when the question is ownership:

function hasOwn(object, key) {
  return Object.hasOwn(object, key);
}

const hasOwnName = hasOwn(item, "name");
const nameAvailable = "name" in item;

Choose Object.hasOwn() when you need a property directly on the object. Choose in when an inherited property should count. Remember that the right-hand operand of in must be an object; using a primitive there throws a TypeError. [S2] These checks do not establish that a property’s value has a particular type or meaning, so validate the value separately when needed.

A practical checklist

  • Decide whether inherited properties should count for your task.

  • Use Object.hasOwn(object, key) for direct ownership.

  • Use key in object when the prototype chain should be included.

  • Test one own property and one inherited property if the distinction matters.

  • Check the property's value separately if presence alone is not enough.

A few more useful answers

Does `in` return false when a property’s value is `undefined`?

No. in checks whether the property is present, and a present property with an undefined value still counts. [S2]

Can `Object.hasOwn()` return true for a property whose value is `null`?

Yes. It checks direct ownership, including when the property value is null or undefined. [S1]

Does `in` check only properties written directly on the object?

No. It checks the object and its prototype chain, so an inherited property can make the result true. [S2]

Sources

References used for this guide.

  1. [S1] Object.hasOwn() — JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] in operator — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work