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