JavaScript in operator

Below is the example of the in operator.

  • Example 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
        function gfg() { 
        // Illustration of in operator
        const array = ['geeks', 'for'
                       'geeks']
       
        // Output of the indexed number
        document.write(0 in array);  
          
        // Output of the Value
        document.write('for' in array);
       
        // output of the Array property
        document.write('length' in array);
        
        gfg(); 
    </script>

    chevron_right

    
    

  • Output:

    true
    false
    true

The in operator is an inbuilt operator in JavaScript which is used to check whether a particular property exists in an object or not. It returns boolean value 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 mentioned above and described below:

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

Return value: This method returns an boolean values :

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

Below examples illustrate the in operator in JavaScript:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Illustration of in operator
    const array = ['geeksforgeeks', 'geeksfor'
                   'geeks', 'geeks1']
  
    // 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('geeksforgeeks' in array)
  
    // output of the Array property
    console.log('length' in array)
</script>

chevron_right


Output:

> true
> true
> false
> false
> false
> true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Illustration of in operator
    const object = { val1: 'Geeksforgeeks'
                     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 = 'GEEKSFORGEEKS';
    }
  
    console.log(object.val1);
</script>

chevron_right


Output:

> true
> false
> "GEEKSFORGEEKS"

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

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Edge
  • Internet Explorer

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.