Check field presence
Validate that an input record explicitly contains a field, even when its value is undefined or null.
JavaScript · Function
Object.hasOwn() returns whether an object contains a specified property as its own property. It excludes inherited properties and works with null-prototype objects.
Call Object.hasOwn with an object and a property key. It converts the inputs as needed and reports whether that key is defined directly on the object, without searching its prototype chain; the key may be a string or a Symbol.
Object.hasOwn(obj, prop)| Name | Type | Description |
|---|---|---|
obj | object | The JavaScript object instance to test. |
prop | string | symbol | The String name or Symbol of the property to test. |
true if the specified object has directly defined the specified property; otherwise false.
in operator for that broader check.const record = Object.create(null);
record.status = undefined;
console.log(Object.hasOwn(record, "status")); // true
console.log(Object.hasOwn(record, "toString")); // false
console.log(Object.hasOwn({ value: null }, "value")); // trueThis checks direct properties, including one whose value is undefined, while excluding inherited properties and working with a null-prototype object.
Validate that an input record explicitly contains a field, even when its value is undefined or null.
Safely inspect dictionaries created with Object.create(null).
Filter enumerable or prototype-chain traversals to direct properties only.
Check keys that are represented by Symbols rather than strings.
Object.prototype.hasOwnProperty()in operatorObject.getOwnPropertyNames()Object.keys()