How to check a key exists in JavaScript object?
There are mainly two methods to check the existence of a key in JavaScript Object. The first one is using “in operator” and the second one is using “hasOwnProperty() method”.
Method 1: Using ‘in’ operator: The in operator returns a boolean value if the specified property is in the object.
Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!
Syntax:
propertyName in object
Example: This example uses “in” operator to check the existence of key in JavaScript object.
<!DOCTYPE html><html> <head> <title> How to check a key exists in JavaScript object? </title></head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to check a key exists in JavaScript object? </b> <p> Click on the button to check if key exists in object </p> Checking for 'name': <p class="output1"></p> Checking for 'remarks': <p class="output2"></p> <button onclick="checkKey()"> Click here </button> <script type="text/javascript"> function checkKey() { // Define an object exampleObj = { id: 1, remarks: 'Good' } // Check for the keys output1 = 'name' in exampleObj; output2 = 'remarks' in exampleObj; document.querySelector('.output1').innerHTML = output1; document.querySelector('.output2').innerHTML = output2; } </script></body> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Method 2: Using the hasOwnProperty() method: The hasOwnProperty() method returns a boolean value which indicates whether the object has the specified property. The required key name could be passed in this function to check if it exists in the object.
Syntax:
object.hasOwnProperty(propertyName)
Example: This example uses hasOwnProperty() method to check the existence of key in JavaScript object.
<!DOCTYPE html><html> <head> <title> How to check a key exists in JavaScript object? </title></head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to check a key exists in JavaScript object? </b> <p> Click on the button to check if key exists in object </p> Checking for 'name': <p class="output1"></p> Checking for 'remarks': <p class="output2"></p> <button onclick="checkKey()"> Click here </button> <script type="text/javascript"> function checkKey() { // Define an object exampleObj = { id: 1, remarks: 'Good' } // Check for the keys output1 = exampleObj.hasOwnProperty('name'); output2 = exampleObj.hasOwnProperty('remarks'); document.querySelector('.output1').innerHTML = output1; document.querySelector('.output2').innerHTML = output2; } </script></body> </html> |
Output:
- Before clicking the button:

- After clicking the button:

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


