Skip to main content

hasIn()

hasIn(object, key): boolean

Checks if path is a direct or inherited property of object.

DEPRECATED

Use the in operator directly instead.


Parameters​

object: object | null | undefined​

The object to query.

key: string​

The key to check.


Returns: boolean​

true if key exists, else false.


See Also​

in operator - MDN


Since​

2.0.0


Also known as​

hasIn (Lodash, es-toolkit, Ramda) · ❌ (Remeda, Radashi, Effect, Modern Dash, Antfu)


Example​

// ❌ Deprecated approach
hasIn({ a: 1 }, 'a'); // => true
hasIn({ a: 1 }, 'toString'); // => true (inherited)

// βœ… Recommended approach
'a' in { a: 1 }; // => true
'toString' in { a: 1 }; // => true (inherited)

How it works?​

Checks if key exists as direct or inherited property.

Inheritance Check​

Common Checks​

ObjectKeyResult
{a: 1}'a' true
{a: 1}'toString' true (inherited)
{a: 1}'b' false
null'a' false

Deprecated: Use the in operator directly.


Use Cases​

Check property exists πŸ“Œβ€‹

Check if property exists on object.

"key" in obj;
// or
Object.hasOwn(obj, "key");

Safe property access​

Check before accessing.

if ("user" in data && "name" in data.user) {
return data.user.name;
}

Optional chaining alternative​

Use optional chaining instead.

data?.user?.name;