The typeof
operator is not a variable. It is an operator. Operators ( + - * / ) do not have any data type.
But, the typeof
operator always returns a string (containing the type of the operand).
The constructor Property
The constructor
property returns the constructor function for all JavaScript variables.
Example
(3.14).constructor // Returns function Number() {[native code]}
false.constructor // Returns function Boolean() {[native code]}
[1,2,3,4].constructor // Returns function Array() {[native code]}
{name:'John',age:34}.constructor // Returns function Object() {[native code]}
new Date().constructor // Returns function Date() {[native code]}
function () {}.constructor // Returns function Function(){[native code]}
You can check the constructor property to find out if an object is an Array
(contains the word "Array"):
Example
return myArray.constructor.toString().indexOf("Array") > -1;
}
Or even simpler, you can check if the object is an Array function:
Example
return myArray.constructor === Array;
}
You can check the constructor property to find out if an object is a Date
(contains the word "Date"):
Example
return myDate.constructor.toString().indexOf("Date") > -1;
}
Or even simpler, you can check if the object is a Date function:
Example
return myDate.constructor === Date;
}
Practice Excercise Practice now