4 Ways To Check if an Object Key Exists
Tags: javascript, webdev, short, tutorial
Published: 2022-02-04 09:34:17



### 1. hasOwnProperty

This will return a boolean value depending on whether the object on which you are calling it has a property with given name as an argument e.g. `obj.hasOwnProperty('keyname')`
 

##### Full Example:

```javascript
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}
 
class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}

let eng = new Engineer("John","Doe","BSC")

console.group("hasOwnProperty");
console.log(eng.hasOwnProperty('firstName'))      // Output: true
console.log(eng.hasOwnProperty('qualification'))  // Output: true
console.log(eng.hasOwnProperty('nonexistantkey')) // Output: false
console.groupEnd();

```

 

#### 2. Object.keys  

Object.keys will return an array of all the keys belonging to the given object.

You can then check the array to see if a particular key exists.  It is the longest of all the methods.

```javascript
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}

class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}

let eng = new Engineer("John","Doe","BSC")

console.group("iteration");
for (const key of Object.keys(eng)) {
   console.log(key)
   //Perform Other Check ..
}

/* Results:
"firstName"
"lastName"
"qualification"
*/

console.groupEnd();

// Or

Object.keys(eng).filter(x => x.includes("keyname"));

```

### 3. in operator

The `in` operator returns true if the property is in the object

```javascript
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}

class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}
let eng = new Engineer("John","Doe","BSC")

console.group("in operator");
console.log('firstName' in eng)      // Output: true
console.log('qualification' in eng)  // Output: true
console.log('nonexistantkey' in eng) // Output: false
console.groupEnd();

```


### 4. Reflect.has()

> Reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.

Therefore using the Reflect api you can check and/or manipulate an Objects properties, methods and variables.

You can use `Reflect.has(targetObject, propertyKey)` method to check if the key exists.

#####Full Example

```javascript
class Employee {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
  }
}
 
class Engineer extends Employee {
    constructor(firstName, lastName, qualification) {
        super(firstName, lastName);
        this.qualification = qualification;
    }
}

let eng = new Engineer("John","Doe","BSC")
console.group("Reflect.has");
console.log(Reflect.has(eng,'firstName'))       // Output: true
console.log(Reflect.has(eng,'qualification'))   // Output: true
console.log(Reflect.has(eng,'nonexistantkey'))  // Output: false
console.groupEnd();
```


#### Slán go fóill
Well that's it, each is simple enough and you can choose whichever to do the task.  
 
Feel free to ask questions, comment or contribute below!

And if you're feeling generous you can buy me a coffee with the link below ( and yes its all for coffee,  I drink a copious amount of it while writing ☕ )  


[![Buy Me A Coffee](https://cdn.buymeacoffee.com/buttons/default-black.png)](https://www.buymeacoffee.com/SUKaQp9)