in Operator

Tests for the existence of a property in an object.

property in object

Arguments

  • property
    Required. An expression that evaluates to a string.

  • object
    Required. Any object.

Remarks

The in operator checks if an object has a property named property. It also checks the object's prototype to see if property is part of the prototype chain. If property is in the object or prototype chain, the in operator returns true, otherwise it returns false.

The in operator should not be confused with the for...in statement.

Note

To test if the object itself has a property, and does not inherit the property from the prototype chain, use the object's hasOwnProperty method.

Example

The following example illustrates a use of the in operator.

function cityName(key : String, cities : Object) : String {
   // Returns a city name associated with an index letter.
   var ret : String = "Key '" + key + "'";
   if( key in cities )
      return ret + " represents " + cities[key] + ".";
   else  // no city indexed by the key
      return ret + " does not represent a city."
}

// Make an object with city names and an index letter.
var cities : Object = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"}

// Look up cities with an index letter.
print(cityName("a",cities));
print(cityName("z",cities));

The output of this code is:

Key 'a' represents Athens.
Key 'z' does not represent a city.

Requirements

Version 1

See Also

Concepts

Operator Precedence

Operator Summary

Reference

for...in Statement

hasOwnProperty Method