

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the "in" operator in JavaScript?
<p>In this article, we are going to explore the '<strong>in</strong>' operator and how to use it in <a href="https://www.tutorialspoint.com/javascript/index.htm" target="_blank">JavaScript</a>. The in operator is an inbuilt operator in JavaScript that is used for checking whether a particular property exists in an object or not. It will return true if the property exists, else false is returned.</p><h2>Syntax</h2><pre class="result notranslate">prop in object</pre><h2>Parameters</h2><p>This function accepts the following parameters as described below −</p><ul class="list"><li><p><strong>prop</strong> − This parameter holds the string or symbol that represents a property name or the array index.</p></li><li><p><strong>object</strong> − This object will be checked if it contains the prop or not.</p></li></ul><p><strong>Return value</strong> − This method will return either true or false if the specified property is found in the object or not.</p><h2>Example 1</h2><p>In the below example, we are going to find if the property exists or not by using the ‘inin’ operator in JavaScript.</p><p><strong># index.html</strong></p><pre class="demo-code notranslate language-javascript" data-lang="javascript"><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></pre><h2>Output</h2><p>The above program will produce the following output in the Console.</p><pre class="result notranslate">true true false false false true</pre><h2>Example 2</h2><p>In the below example, we illustrate the <strong>in</strong> operator.</p><p><strong># index.html</strong></p><pre class="demo-code notranslate language-javascript" data-lang="javascript"><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></pre><h2>Output</h2><p>The above program will produce the following result in the Console.</p><pre class="result notranslate">true false Steve</pre>
- Related Questions & Answers
- How to use the ?: conditional operator in C#?
- How to use the instanceof operator in Java?
- How to use Operator Overloading in C#?
- How to use spread operator to join two or more arrays in JavaScript?
- How to use Null Coalescing Operator (??) in C#?
- How to use an assignment operator in C#?
- How to use *= operator in jQuery attribute selector?
- How to use INTERSECT operator in Android sqlite?
- The new operator in JavaScript
- How to use ‘as’ operator in C#?
- How to use ‘is’ operator in C#?
- How do I use the conditional operator in C/C++?
- How to use comparison operator for numeric string in MySQL?
- How to work with delete operator in JavaScript?
- How to use the debugger keyword in JavaScript?
Advertisements