The in operator is a built-in operator in JavaScript, used to check whether a specific property exists in an object. It returns Boolean true if the specified property is in an object, otherwise it returns false.

Syntax:

prop in object

Parameters: This function accepts the following parameter as shown above and described in detail below:

  • prop: This parameter holds the string that represents a property name or array index.
  • object: This parameter is an Object which is to be checked whether it contains the prop.

Return value: This method returns a boolean value:

  • true: The value true is returned if the specified property is found in an object.
  • false: The value false is returned if the specified property is not found in an object.

Example 1: Below is an example of the in-operator.

JavaScript

<script>
    function test() {
    // Illustration of in operator
    const array = ['Codes', 'for',
                'Config']
     
    // Output of the indexed number
    console.log(0 in array);
     
    // Output of the Value
    console.log('for' in array);
     
    // output of the Array property
    console.log('length' in array);
    }
    test();
</script>

Output:

true
false
true

Example 2: This example shows the use of the in operator in JavaScript.

JavaScript

<script>
    // Illustration of in operator
    const array = ['CodeConfig', 'CodeConfig.in',
                'Codes', 'Codes1']
     
    // Output of the indexed number
    console.log(0 in array)        
    console.log(2 in array)    
    console.log(5 in array)    
     
    // Output of the Value
    console.log('for' in array)
    console.log('CodeConfig.in' in array)
     
    // output of the Array property
    console.log('length' in array)
</script>

Output:

true
true
false
false
false
true

Example 3: Let’s check the use of the in operator with the help of example in JavaScript.

JavaScript

<script>
    // Illustration of in operator
    const object = { val1: 'CodeConfig.in',
                    val2: 'JavaScript',
                    val3: 'operator',
                    val4: 'in'};
     
    console.log('val1' in object);
     
    delete object.val1;
    console.log('val1' in object);
     
    if ('val1' in object === false) {
    object.val1 = 'CodeConfig.in';
    }
     
    console.log(object.val1);
</script>

Output:

true
false
"CodeConfig.in"

Supported Browsers: The browsers supported by JavaScript in operator are listed below:

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Edge
  • Internet Explorer
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, CodeConfig.in Courses are your key to success. We provide top-quality content at no cost, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Explore More

How to Center a Popup Window on Screen

JavaScript window.open() method is used to open a popup window. This popup window will be placed in the center of the screen. This example creates the pop-up window without placing it into

How to hide URL in the popup window opened using window.open

If you are using below code to open a popup window in your web page, then address must be appearing in you pop up window and f you want to

window.showModalDialog is deprecated in Edge and Chrome

If you have a website being compatible with Edge/Chrome and in past it was only compatible with IE 11 and if you are using window.showModalDailog in your JavaScript code, then