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โ
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โ
| Object | Key | Result |
|---|---|---|
{a: 1} | 'a' | true |
{a: 1} | 'toString' | true (inherited) |
{a: 1} | 'b' | false |
null | 'a' | false |
Deprecated: Use the
inoperator 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;
true
false
Deprecated: Use the