has()
has<
T>(object,key):boolean
Checks if an object has a property as its own (not inherited).
Works with objects created via Object.create(null).
Undefined/null values still return true if the property exists.
Type Parameters
T: T extends object
The type of the object.
Parameters
object: T
The object to check.
key: PropertyKey
The property key to check for.
Returns: boolean
true if the object has the own property, false otherwise.
See Also
Object.hasOwn (ES2022 native alternative)
Since
2.0.0
Performance
O(1) - Direct property lookup via Object.prototype.hasOwnProperty.
Also known as
has (Lodash, es-toolkit, Ramda, Effect) · ❌ (Remeda, Radashi, Modern Dash, Antfu)
Example
const obj = { a: 1, b: undefined };
has(obj, 'a'); // => true
has(obj, 'b'); // => true (property exists)
has(obj, 'c'); // => false
const bare = Object.create(null);
bare.x = 42;
has(bare, 'x'); // => true
How it works?
Checks if an object has a property as its own (not inherited).
Own vs Inherited
Works with Object.create(null)
Unlike obj.hasOwnProperty(), has() works even when the prototype chain is broken.
Use Cases
Check property existence 📌
Safely check if an object has a property as its own key (not inherited). Essential for robust validation against prototypes.
if (has(data, 'result')) {
process(data.result);
}
Detect explicit undefined
Distinguish between a property that exists but is undefined vs a missing property.
get() returns undefined in both cases, has() differentiates.
const obj = { start: undefined };
has(obj, 'start'); // true
has(obj, 'end'); // false
Validate dynamic keys
Check for the presence of a key when the key name is variable. Useful in loops or dynamic logic.
const key = getCurrentKey();
if (!has(cache, key)) {
loadData(key);
}