JavaScript instanceof and typeof are commonly used to determine whether a variable is empty, or what type it is. But there is a difference between them:
First, typeof
1, meaning: typeof returns a string of the data type of an expression, and returns the result as JS basic data type, including number,boolean,string,object,undefined,function. Syntax for typeof ( Data) or typeof data.
We can use TypeOf to get the existence of a variable, such as:
if (typeof a!='undefined') {alert ('ok ');}
It is best not to use if (a), because if a does not exist (undeclared) then it will go wrong, and for special objects such as Array,null, use TypeOf to return object, which is the limitation of TypeOf.
Second, instanceof
Instanceof to determine whether an object is a data type, or whether a variable is an instance of an object; Returns a Boolean type
The syntax is o instanceof A.
var o={};18alert ("H instanceof Person:" + (H instanceof person)); // true19alert ("H instanceof object:" + (H instanceof object)); // true20alert ("o instanceof object:" + (o instanceof object)); // true
JavaScript Advanced Programming (v): JS keyword instanceof and typeof use