{{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
delete
- {{jsxref("Object.prototype.hasOwnProperty()")}}
- {{jsxref("Reflect.has()")}}
- Enumerability and ownership of properties