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'checkmark true
{a: 1}'toString'checkmark true (inherited)
{a: 1}'b'cross false
null'a'cross false

warning 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;