The typeof operator returns the type information as a string. There are six possible typeof return values: "Number," "String," "Boolean," "Object," "function," and "undefined." We can use TypeOf to get the existence of a variable, such as if (typeof a!= "undefined") {}, and not to use if (a) because if a does not exist (not declared) there will be an error, for array, A special object such as Null uses TypeOf to return object, which is the limitation of TypeOf.
Parentheses in the typeof syntax are optional.
if (document.mylist.length!= "undefined") {} This usage is incorrect.
The correct is if (typeof (Document.mylist.length)!= "undefined") {}
Or if (!isnan (Document.mylist.length)) {}
The typeof operand is undefined, and the return is "undefined".
The number of operands is typeof (x) = "Number"
String typeof (X) = "string"
Boolean value typeof (X) = "Boolean"
objects, arrays, and Null typeof (X) = "Object"
function typeof (X) = "function"
The typeof operator returns a String that represents the data type of an expression.
Possible strings are: "Number", "string", "Boolean", "Object", "function", and "undefined".
Such as:
The code is as follows |
Copy Code |
Alert (typeof (123));//typeof (123) returns "Number" Alert (typeof ("123″));//typeof (" 123″) returns "string" |
For Array,null and other special objects to use TypeOf all return object, this is the limitations of TypeOf.
If we want to get an object to be an array, or to determine whether a variable is an instance of an object, choose to use instanceof. Instanceof is used to determine whether a variable is an instance of an object, such as the Var a=new array (); alert (a instanceof array); Returns true while alert (a Instanceof object) Also returns true, because array is a subclass of object. Again, the function test () {};var a=new test (), alert (a instanceof test) returns True.
Friendly Tips
A Instanceof object gets true not because the array is a child of object, but because the prototype property of the array is constructed at the parent of the Object,array is a function