JavaScript Program to Check if a Key Exists in an Object

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Check if Key Exists in Object Using in Operator

Output

The key exists.

In the above program, the in operator is used to check if a key exists in an object. The in operator returns true if the specified key is in the object, otherwise it returns false.


Example 2: Check if Key Exists in Object Using hasOwnProperty()

Output

The key exists.

In the above program, the hasOwnProperty() method is used to check if a key exists in an object. The hasOwnProperty() method returns true if the specified key is in the object, otherwise it returns false.