CKTIS / JavaScript
Object.hasOwn: test direct property ownership
Object.hasOwn returns true when a key is an object’s own property, regardless of that property’s value or enumerability.
Version: ECMAScript 2022 onward; example target: Node.js 24.22.2
Syntax
Object.hasOwn(obj, prop)Object.hasOwn(obj, prop) first performs ToObject(obj) and converts prop with ToPropertyKey, so both string and Symbol keys work. It returns a boolean saying whether the key’s descriptor belongs directly to the object. Inherited properties are ignored, and an existing undefined value is not confused with an absent property.
The test covers data and accessor properties, including non-enumerable ones. For an ordinary object it neither reads the value nor invokes a getter. It is safer than obj.hasOwnProperty(prop) when an object has a null prototype or shadows hasOwnProperty. The in operator and Reflect.has answer a different question because they search the prototype chain.
The method has existed since ECMAScript 2022. null and undefined cause TypeError; other primitives are boxed, so a string index such as 1 in "abc" can be an own property of the wrapper. Key conversion can execute code and throw. For a Proxy, the getOwnPropertyDescriptor trap runs; its exception or a target-invariant violation can produce TypeError.
Examples
const token = Symbol("token");
const proto = { inherited: 1 };
const record = Object.create(proto);
Object.defineProperty(record, "hidden", { value: 2 });
record[token] = undefined;
console.log(Object.hasOwn(record, "hidden"), Object.hasOwn(record, "inherited"));
console.log(Object.hasOwn(record, token), Object.hasOwn(record, "missing"));
console.log(Object.hasOwn("abc", 1));
try { Object.hasOwn(null, "x"); } catch (error) { console.log(error.name); }Result
true false true false true TypeError
The example distinguishes own, inherited, Symbol, missing, and boxed-string index properties.