How to use the "in" operator in JavaScript?

JavascriptWeb DevelopmentFront End Technology

<p>In this article, we are going to explore the &#39;<strong>in</strong>&#39; 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 &minus;</p><ul class="list"><li><p><strong>prop</strong> &minus; This parameter holds the string or symbol that represents a property name or the array index.</p></li><li><p><strong>object</strong> &minus; This object will be checked if it contains the prop or not.</p></li></ul><p><strong>Return value</strong> &minus; 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 &lsquo;inin&rsquo; operator in JavaScript.</p><p><strong># index.html</strong></p><pre class="demo-code notranslate language-javascript" data-lang="javascript">&lt;html&gt; &lt;head&gt; &nbsp; &nbsp;&lt;title&gt;IN operator&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &nbsp; &nbsp;&lt;h1 style=&quot;color: red;&quot;&gt; &nbsp; &nbsp; &nbsp; Welcome To Tutorials Point &nbsp; &nbsp;&lt;/h1&gt; &nbsp; &nbsp;&lt;script&gt; &nbsp; &nbsp; &nbsp; // Illustration of in operator &nbsp; &nbsp; &nbsp; const array = [&#39;key&#39;, &#39;value&#39;, &#39;title&#39;, &#39;TutorialsPoint&#39;] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output of the indexed number &nbsp; &nbsp; &nbsp; console.log(0 in array) //true &nbsp; &nbsp; &nbsp; console.log(2 in array) //true &nbsp; &nbsp; &nbsp; console.log(5 in array) //false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output of the Value &nbsp; &nbsp; &nbsp; // you must specify the index number, not the value at that index &nbsp; &nbsp; &nbsp; console.log(&#39;key&#39; in array) //false &nbsp; &nbsp; &nbsp; console.log(&#39;TutorialsPoint&#39; in array) // false &nbsp; &nbsp; &nbsp; // output of the Array property &nbsp; &nbsp; &nbsp; console.log(&#39;length&#39; in array) &nbsp; &nbsp;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</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">&lt;html&gt; &lt;head&gt; &nbsp; &nbsp;&lt;title&gt;IN operator&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &nbsp; &nbsp;&lt;h1 style=&quot;color: red;&quot;&gt; &nbsp; &nbsp; &nbsp; Welcome To Tutorials Point &nbsp; &nbsp;&lt;/h1&gt; &nbsp; &nbsp;&lt;script&gt; &nbsp; &nbsp; &nbsp; // Illustration of in operator &nbsp; &nbsp; &nbsp; const student = { name: &#39;Bill&#39;, class: &#39;IX&#39;, subjects: &#39;PCM&#39;, age: &#39;16&#39; }; &nbsp; &nbsp; &nbsp; console.log(&#39;name&#39; in student); &nbsp; &nbsp; &nbsp; delete student.name; &nbsp; &nbsp; &nbsp; console.log(&#39;name&#39; in student); &nbsp; &nbsp; &nbsp; if (&#39;name&#39; in student === false) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;student.name = &#39;Steve&#39;; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; console.log(student.name); &nbsp; &nbsp;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</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>
raja
Updated on 28-Apr-2022 11:59:56

Advertisements