Definition:
The in operator is used to determine that an attribute belongs to an object, either as a direct property of an object or as an inherited property through prototype. (See hasOwnProperty) Precautions :
n for a Generic object property, you need to specify the name of the property with a string
Such as:
var Mycar = {make: "Honda", Model: "Accord", year:1998};
' Make ' in Mycar//returns True
"Model" in Mycar//returns True
n an indexed value that specifies a numeric form for an array property to represent the property name of the array (except for intrinsic attributes, such as length).
Arrays
var trees = new Array ("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 in that index)
' Length ' in Trees//returns True (the length is a Array property)
The right side of N in must be an object, such as: You can specify a form that is generated with the string constructor, but you cannot specify a direct amount of string:
var color1 = new String ("green");
' Length ' in Color1//returns True
var color2 = "Coral";
"Length" in Color2//generates an error (color isn't a String object)
n If you Delete an attribute using the delete operator, it returns falseif you use the in check again,such as:
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
n If you set a property value to undefinedbut do not use the delete operator, using in check will return true.
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