How to check a key exists in JavaScript object? - GeeksforGeeks

26 Jul, 2021
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:
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.
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?