Object/Since
1.38.0has
Checks whether an object has its own property with the specified key.
has(object, key):booleanOwn properties
The function checks only whether the key belongs directly to the object. Properties inherited through the prototype chain are not considered.
PropertyKey
The key parameter uses the PropertyKey type, so string, number, and symbol keys can be checked.
Undefined properties
A property whose value is undefined still counts as an existing property. The function checks whether the key exists, not whether its value is defined.
Parameters
objectTrequiredThe object to check.
keyPropertyKeyrequiredThe property key to check. It can be a string, number, or symbol.
Returns
booleantrue if the object has the specified key as its own property; otherwise, false.
Examples
01
Basic usage
TypeScript
const user = {
name: "Amir",
age: 22,
};
has(user, "name");
// true
has(user, "email");
// false02
Own properties only
TypeScript
const parent = {
name: "Amir",
};
const user = Object.create(parent);
has(user, "name");
// false03
Inherited properties
TypeScript
const parent = {
role: "admin",
};
const user = Object.create(parent);
has(user, "role");
// false04
Undefined values
TypeScript
const data = {
value: undefined,
};
has(data, "value");
// true05
Null values
TypeScript
const data = {
value: null,
};
has(data, "value");
// true06
Symbol keys
TypeScript
const id = Symbol("id");
const user = {
[id]: 123,
};
has(user, id);
// true07
Numeric keys
TypeScript
const data = {
1: "one",
};
has(data, 1);
// true08
Falsy values
TypeScript
const data = {
count: 0,
active: false,
name: "",
};
has(data, "count");
// true
has(data, "active");
// true
has(data, "name");
// trueBuilt with@cyref/js·Official documentation