Home >Web Front-end >JS Tutorial >What are typeof and instanceof in js? What's the difference?
This article mainly introduces to you the difference between typeof and instanceof in js.
First of all, everyone should briefly understand what typeof is?
typeof is a unary operation, placed before an operand, and the operand can be of any type. Its return value is a string describing the type of the operand. typeof can be used to detect the data type of a given variable.
What is instanceof?
The instanceof operator is used to determine whether the object pointed to by the prototype attribute of a constructor exists on the prototype chain of another object to be detected. Generally speaking, using instanceof is to determine whether an instance belongs to a certain type.
The similarities between typeof and instanceof in js:
Typeof and instanceof in JavaScript are often used to determine whether a variable is empty or what type it is.
Differences:
1. The definition and usage of typeof:
The return value is a string , used to describe the data type of the variable.
Specific usage details:
1. typeof generally can only return the following results:
'undefined': This value is undefined.
'boolean': This value is a Boolean value.
'string' : This value is a string.
'number' : This value is a numeric value.
'object': This value is an object or null.
'function' : This value is a function.
2. Use typeof to get whether a variable exists, such as
if(typeof a!="undefined"){alert("ok")}
instead of using if(a) because if a does not exist (not declared), an error will occur.
3. For special objects such as Array and Null, typeof always returns object. This is the limitation of typeof.
2. Instanceof definition and usage:
Instanceof definition and usage: instanceof is used to determine whether a variable belongs to an instance of an object. It can also be used to determine whether the prototype attribute of a certain constructor exists on the prototype chain of another object to be detected.
Example:
a instanceof b?alert("true"):alert("false"); //a是b的实例?真:假
var a=new Array();alert(a instanceof Array);
will return
true, and
alert(a instanceof Object)
will also return true;
This is because Array is an object subcategory.
Another example:
function test(){};var a=new test();alert(a instanceof test)
will return object.
Test:
var a=new Array();if (a instanceof Object) alert('Y');else alert('N');
got 'Y', and
if (window instanceof Object) alert('Y');else alert('N');
got 'N'.
So, the object tested by instanceof here refers to the object in js syntax, not the dom model object.
There will be some differences when using typeof:
alert(typeof(window))
You will get object.
This article is about the difference between typeof and instanceof in js. I hope it will be helpful to friends in need!
If you want to know more front-end knowledge points, you can follow the PHP Chinese website JavaScript Video Tutorial, Bootstrap Video Tutorial and other related tutorials. Welcome everyone to refer to and learn!
The above is the detailed content of What are typeof and instanceof in js? What's the difference?. For more information, please follow other related articles on the PHP Chinese website!