How to check if a key exists in a JavaScript object?

If you are working with the javascript objects which are the collections of key-value pairs, there are multiple ways to find if a key exists in a javascript object or not. Here we will explore all the ways with a comprehensive understanding of each approach.

Different methods to check if a key exists in a javascript object:

Method 1 : USE the “in” Operator

The simplest way to check if a key exists in an object is the “in” operator, it returns “true” value if a key exists and “false” otherwise. To demonstrate how to use “in” operator, we will use “if” statement.

Here is an e.g. to check if a key exists in an object through “in” operator-

const student = {
name: “rohit”,
age: “15”
};

if (“name” in student) {
console.log(“The ‘name’ property exists in the employee object.”);
}

Output:- The ‘name’ property exists in the employee object.

Method 2 : USE the hasOwnproperty() method

Another way to check if a key exists in an object is “hasOwnproperty()” method, it returns “true” value if a key is present and “false” value if not. This method specifies that the object has its own property.

Here is an e.g. to check if a key exists in an object through “hasOwnproperty()” method.

const student = {
name: “marin”,
age: 25,

};

if (student.hasOwnProperty(“age”)) {
console.log(“The ‘age’ property exists in the student object.”);
}

Output:- The ‘age’ property exists in the student object.

Method 3: USE the object.key() and includes() methods.

The object.key() and includes() method is effective for checking the existence of a specific key within an object when you know the key’s name in advance. Here the object.key() funtion returns an array of object keys and includes() funtion returns a boolean depending on key existence.

See also  Workers Compensation Insurance Georgia

Here is an e.g. to check if a key exists in an object through object.key() and includes() methods.

const student = {
name: “Deepak”,
age: “13”

};

if (Object.keys(student).includes(“name”)) {
console.log(“The ‘name’ property exists in the student object.”);
}

Output:- The ‘name’ property exists in the student object.

Method 4: USE the object.getownpropertysymbols() method and includes() method

One more way to check if a key exists in an object by object.getownpropertysymbols() method and includes() method, it returns an array of all symbol properties that are present in a given object. This method allows you to check for the existence of symbol keys in an object and is particularly useful when dealing with unique properties that are not accessible using regular string keys.

Here is the e.g. to check if a key exists in an object through the object.getownpropertysymbols() method and includes() method.

const age = Symbol(‘age’);
const student = {
name: ‘nil’,
};

const hasAge = Object.getOwnPropertySymbols(student).includes(age);
console.log(hasAge);

Output:- true

Method 5 : USE the object.getownpropertyNames() method and includes() method

Another approach to check if a key exists in an object is by object.getownpropertyNames() method and includes() method, which returns an array of all the properties that are present in a given object except symbol-based properties.

Here is the e.g. to check if a key exists in an object through the object.getownpropertyNames() method.

const age = Symbol(‘age’);
const student = {
name: ‘dell’,
};

const hasAge = Object.getOwnPropertyNames(student).includes(‘age’);
console.log(hasAge);

Output:- false

Method 6 : Using Property Accessors

You can access the properties of objects through dot notation or bracket notation. With these Property Accessors, you can use the strict inequality operator (!==) to find out a key present in the operator.

See also  Workers Compensation Insurance Colorado

Here is the e.g. to check if a key exists in an object through the Property Accessors.

const student = {
name: ‘dell’,
age: 30
};

console.log(student.age !== undefined); // true
console.log(student[“age”] !== undefined); // true

Output:- true

Method 7 : USE the Reflect.ownkeys() method and includes() method

The Reflect.ownKeys() methods returns an array of all the property keys (including both string and symbol keys) of an object. This method is particularly useful if you want to check for the existence of any key within the object without specifying a particular key in advance.

Here is the e.g. to check if a key exists in an object through the Reflect.ownkeys() method

const age = Symbol(‘age’);
const student = {
name: ‘gigglr’,
};

const hasName = Reflect.ownKeys(student).includes(‘name’);
console.log(hasName);

Output:- true

Above we have mentioned all of the possible ways or methods to check if a key exists in an object in Javascript. We hope this article will help you to find the solution to your problem.

Read More Link :

How to disable end-to-end encryption in WhatsApp