Home > Article > Web Front-end > How to use 'in' operator in JavaScript?
In this article, we will explore the “in” operator and how to use it in JavaScript. The in operator is a built-in operator in JavaScript that is used to check whether a specific property exists in an object. Returns true if the property exists, false otherwise.
prop in object
This function accepts the following parameters as mentioned below-
prop - This parameter holds a string or symbol representing the property name or array index.
object - will check if the object contains prop .
Return value - if The specified property is found in the object, this method returns true or false.
In the following example, we will use the 'inin' operator in JavaScript to find whether a property exists.
# index .html
<html> <head> <title>IN operator</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> // Illustration of in operator const array = ['key', 'value', 'title', 'TutorialsPoint'] // Output of the indexed number console.log(0 in array) //true console.log(2 in array) //true console.log(5 in array) //false // Output of the Value // you must specify the index number, not the value at that index console.log('key' in array) //false console.log('TutorialsPoint' in array) // false // output of the Array property console.log('length' in array) </script> </body> </html>
The above program will produce the following output in the console.
true true false false false true
In the following example, we demonstrate the in operator.
# index.html p>
<html> <head> <title>IN operator</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> // Illustration of in operator const student = { name: 'Bill', class: 'IX', subjects: 'PCM', age: '16' }; console.log('name' in student); delete student.name; console.log('name' in student); if ('name' in student === false) { student.name = 'Steve'; } console.log(student.name); </script> </body> </html>
The above program will produce the following results in the console.
true false Steve
The above is the detailed content of How to use 'in' operator in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!