Export (0) Print
Expand All

in Operator

Tests for the existence of a property in an object.

property in object

property

Required. An expression that evaluates to a string.

object

Required. Any object.

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.

NoteNote

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.

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.

Community Additions

ADD
Show:
© 2015 Microsoft