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.
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:

Recommended Posts:
- JavaScript | Check if a key exists inside a JSON object
- How to access an object having spaces in the object's key using JavaScript ?
- How to make a key fire when key is pressed using JavaScript ?
- How to check whether an object exists in javascript ?
- How to check a key exists in an array in PHP ?
- How to check a JavaScript Object is a DOM Object ?
- How to add key/value pair to a JavaScript object?
- JavaScript | Set object key by variable
- Rename object key in JavaScript
- How to get a key in a JavaScript object by its value ?
- How to get the first key name of a JavaScript object ?
- How to use a variable for a key in a JavaScript object literal?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
- How to check the existence of key in an object using AngularJS ?
- Difference between Primary key and Super key
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
- JavaScript | Check if element exists in the visible DOM
- How to Check Mentioned File Exists or not using JavaScript/jQuery ?
- How to Check an Element with Specific ID Exists using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


