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);
}
Detect high contrast mode for accessible renderingβ
Check if the browser is in high contrast / forced colors mode. Essential for design systems that need to adapt visuals for accessibility.
const hasHighContrast = has(window, "matchMedia") &&
window.matchMedia("(forced-colors: active)").matches;
if (hasHighContrast) {
// Use system colors instead of custom palette
applyHighContrastTheme();
}
Check for Resize Observer support before observingβ
Verify ResizeObserver exists before using it for responsive components. Critical for CDK-style observers that adapt to container size changes.
const canObserveResize = has(window, "ResizeObserver");
if (canObserveResize) {
const observer = new ResizeObserver((entries) => {
for (const entry of entries) {
updateComponentSize(entry.contentRect);
}
});
observer.observe(containerElement);
} else {
// Fallback: listen to window resize
window.addEventListener("resize", () => updateComponentSize(containerElement.getBoundingClientRect()));
}
Check feature support before using browser APIsβ
Verify a browser API exists before calling it. Essential for progressive enhancement and cross-browser compatibility.
const canShare = has(navigator, "share");
const canVibrate = has(navigator, "vibrate");
if (canShare) {
navigator.share({ title: "Check this out", url: window.location.href });
} else {
showCopyLinkFallback();
}