Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to check a key exists in JavaScript object ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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. 

html




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

Output:

How to check a key exists in JavaScript object?

How to check a key exists in JavaScript object?

Method 2: Using the hasOwnProperty() method.

The hasOwnProperty() method returns a boolean value that 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 a key in a JavaScript object. 

html




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

Output:

How to check a key exists in JavaScript object?

How to check a key exists in JavaScript object?

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.


My Personal Notes arrow_drop_up
Last Updated : 12 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials