Skip to main content

has()

has<T>(object, key): boolean

Checks if an object has a property as its own (not inherited).

note

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();
}