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.

Syntax:

propertyName in object

Example: This example uses “in” operator to check the existence of key in JavaScript object.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

  • Before clicking the button:
    using_in-before
  • After clicking the button:
    using_in-after

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

  • Before clicking the button:
    hasOwnProperty-before
  • After clicking the button:
    hasOwnProperty-after


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

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.