Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 974755 of in operator

  • Revision slug: Web/JavaScript/Reference/Operators/in
  • Revision title: in operator
  • Revision id: 974755
  • Created:
  • Creator: lewisje
  • Is current revision? No
  • Comment I don't know how to indicate supported versions, but I do know that symbols work as the left operands, and that even odd values for the left operand, like null, booleans, and objects, are converted to strings.

Revision Content

{{jsSidebar("Operators")}}

The in operator returns true if the specified property is in the specified object.

Syntax

prop in objectName

Parameters

prop
A string or symbol representing a property name or array index (non-symbols will be coerced to strings).
objectName
Name of an object.

Description

The following examples show some uses of the in operator.

// Arrays
var trees = ["redwood", "bay", "cedar", "oak", "maple"];
0 in trees        // returns true
3 in trees        // returns true
6 in trees        // returns false
"bay" in trees    // returns false (you must specify the 
                  // index number, not the value at that index)
"length" in trees // returns true (length is an Array property)
Symbol.iterator in trees // returns true (arrays are iterable, works only in ES6+)

// Predefined objects
"PI" in Math          // returns true

// Custom objects
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar  // returns true
"model" in mycar // returns true

You must specify an object on the right side of the in operator. For example, you can specify a string created with the String constructor, but you cannot specify a string literal.

var color1 = new String("green");
"length" in color1 // returns true

var color2 = "coral";
// generates an error (color2 is not a String object)
"length" in color2

Using in with deleted or undefined properties

If you delete a property with the delete operator, the in operator returns false for that property.

var mycar = {make: "Honda", model: "Accord", year: 1998};
delete mycar.make;
"make" in mycar;  // returns false

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
3 in trees; // returns false

If you set a property to {{jsxref("Global_Objects/undefined", "undefined")}} but do not delete it, the in operator returns true for that property.

var mycar = {make: "Honda", model: "Accord", year: 1998};
mycar.make = undefined;
"make" in mycar;  // returns true
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
3 in trees; // returns true

Inherited properties

The in operator returns true for properties in the prototype chain.

"toString" in {}; // returns true

Specifications

Specification Status Comment
{{SpecName('ESDraft', '#sec-relational-operators', 'Relational Operators')}} {{Spec2('ESDraft')}}  
{{SpecName('ES6', '#sec-relational-operators', 'Relational Operators')}} {{Spec2('ES6')}}  
{{SpecName('ES5.1', '#sec-11.8.7', 'The in Operator')}} {{Spec2('ES5.1')}}  
{{SpecName('ES3', '#sec-11.8.7', 'The in Operator')}} {{Spec2('ES3')}} Initial definition. Implemented in JavaScript 1.4.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Microsoft Edge Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

Revision Source

<div>{{jsSidebar("Operators")}}</div>

<p>The <strong><code>in</code> operator</strong> returns <code>true</code> if the specified property is in the specified object.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<em>prop</em> in <em>objectName</em></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>prop</code></dt>
 <dd>A string or symbol representing a property name or array index (non-symbols will be coerced to strings).</dd>
</dl>

<dl>
 <dt><code>objectName</code></dt>
 <dd>Name of an object.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>The following examples show some uses of the <code>in</code> operator.</p>

<pre class="brush:js">
// Arrays
var trees = ["redwood", "bay", "cedar", "oak", "maple"];
0 in trees        // returns true
3 in trees        // returns true
6 in trees        // returns false
"bay" in trees    // returns false (you must specify the 
                  // index number, not the value at that index)
"length" in trees // returns true (length is an Array property)
Symbol.iterator in trees // returns true (arrays are iterable, works only in ES6+)

// Predefined objects
"PI" in Math          // returns true

// Custom objects
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar  // returns true
"model" in mycar // returns true
</pre>

<p>You must specify an object on the right side of the <code>in</code> operator. For example, you can specify a string created with the <code>String</code> constructor, but you cannot specify a string literal.</p>

<pre class="brush:js">
var color1 = new String("green");
"length" in color1 // returns true

var color2 = "coral";
// generates an error (color2 is not a String object)
"length" in color2
</pre>

<h3 id="Using_in_with_deleted_or_undefined_properties">Using <code>in</code> with deleted or undefined properties</h3>

<p>If you delete a property with the <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete" title="en-US/docs/JavaScript/Reference/Operators/Special/delete">delete</a></code> operator, the <code>in</code> operator returns <code>false</code> for that property.</p>

<pre class="brush:js">
var mycar = {make: "Honda", model: "Accord", year: 1998};
delete mycar.make;
"make" in mycar;  // returns false

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
3 in trees; // returns false
</pre>

<p>If you set a property to {{jsxref("Global_Objects/undefined", "undefined")}} but do not delete it, the <code>in</code> operator returns true for that property.</p>

<pre class="brush:js">
var mycar = {make: "Honda", model: "Accord", year: 1998};
mycar.make = undefined;
"make" in mycar;  // returns true
</pre>

<pre class="brush:js">
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
3 in trees; // returns true
</pre>

<h3 id="Inherited_properties">Inherited properties</h3>

<p>The <code>in</code> operator returns <code>true</code> for properties in the prototype chain.</p>

<pre class="brush:js">
"toString" in {}; // returns true
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-relational-operators', 'Relational Operators')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-relational-operators', 'Relational Operators')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-11.8.7', 'The in Operator')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES3', '#sec-11.8.7', 'The in Operator')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.4.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Microsoft Edge</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete" title="en-US/docs/JavaScript/Reference/Operators/Special/delete">delete</a></code></li>
 <li>{{jsxref("Object.prototype.hasOwnProperty()")}}</li>
 <li>{{jsxref("Reflect.has()")}}</li>
 <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties" title="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
</ul>
Revert to this revision