in special operator

Property exists in object or its prototype chain.

Description

The in special operator returns true if the specified property is in the specified object or its prototype chain, or false otherwise.

  • For any property of an object that is marked as undefined will still return true as the property still exists.

Syntax

Signature Description
propertyName in objectNameThe in special operator returns true if the specified property is in the specified object or its prototype chain, or false otherwise.

Parameters

Parameter Description
propertyNameThe name of the property.
  • For Array objects - An integer representing the element from the zero based index or a string literal representing the name of the property.
  • For other objects - a string literal representing the name of the property.
objectNameThe object to locate the property in.

Examples

Following are examples of the in special operator.


// The in special operator.
var anArray = new Array('fred','ned','ted','jed');
var aString = new String('awesome');
var inArray = new Array(6); 

inArray[0] = 'fred' in inArray;  // false, need to search element index 
inArray[1] = 0 in inArray; // true, this element index exists 
inArray[2] = 4 in inArray; // false, this element index does not exist 
inArray[3] = 'constructor' in inArray; // true, constructor Array property 
inArray[4] = 'length' in aString; // true, length is a String property 
inArray[5] = 'prototype' in Object; // // true, prototype Object property  

alert(inArray); 

Press the button below to action the above code: