CyrefJS
CCyrefJS
فا
Back to documentation
Object/Since 1.38.0

has

Checks whether an object has its own property with the specified key.

has(object, key):boolean

Own 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

objectTrequired

The object to check.

keyPropertyKeyrequired

The property key to check. It can be a string, number, or symbol.

Returns

boolean

true 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");
// false
02

Own properties only

TypeScript
const parent = {
  name: "Amir",
};

const user = Object.create(parent);

has(user, "name");
// false
03

Inherited properties

TypeScript
const parent = {
  role: "admin",
};

const user = Object.create(parent);

has(user, "role");
// false
04

Undefined values

TypeScript
const data = {
  value: undefined,
};

has(data, "value");
// true
05

Null values

TypeScript
const data = {
  value: null,
};

has(data, "value");
// true
06

Symbol keys

TypeScript
const id = Symbol("id");

const user = {
  [id]: 123,
};

has(user, id);
// true
07

Numeric keys

TypeScript
const data = {
  1: "one",
};

has(data, 1);
// true
08

Falsy values

TypeScript
const data = {
  count: 0,
  active: false,
  name: "",
};

has(data, "count");
// true

has(data, "active");
// true

has(data, "name");
// true
Built with@cyref/js·Official documentation