JavaScript in operator is an inbuilt operator which is used to check whether a particular property exists in an object or not. It returns a boolean value true if the specified property is in an object, otherwise, it returns false.
Syntax:
prop in object
Parameters:
- prop: This parameter holds the string or symbol that represents a property name or array index.
- object: This parameter is an Object that is to be checked whether it contains the prop.
Return value:
This method returns a boolean value. The value true is returned if the specified property is found in an object, else it returns false.
Example 1: Below is an example of the in-operator.
Javascript
function gfg() {
const array = [ 'geeks' , 'for' ,
'geeks' ]
console.log(0 in array);
console.log( 'for' in array);
console.log( 'length' in array);
}
gfg();
|
Example 2: This example shows the use of the in operator in Javascript.
Javascript
const array = [ 'geeksforgeeks' , 'geeksfor' ,
'geeks' , 'geeks1' ]
console.log(0 in array)
console.log(2 in array)
console.log(5 in array)
console.log( 'for' in array)
console.log( 'geeksforgeeks' in array)
console.log( 'length' in array)
|
Output
true
true
false
false
false
true
Example 3: This example shows the use of the in operator in Javascript.
Javascript
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);
|
Output
true
false
GEEKSFORGEEKS
Supported Browsers:
- Google Chrome
- Firefox
- Opera
- Safari
- Edge
- Internet Explorer
We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.
Last Updated :
13 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...