Home > Web Front-end > JS Tutorial > body text

An in-depth explanation of the difference between typeof and instanceof in JS

亚连
Release: 2018-05-19 15:15:51
Original
1366 people have browsed it

Typeof and instanceof in JavaScript are often used to determine whether a variable is empty or what type it is. But there are still differences between them. Friends in need can refer to

typeof and instanceof in JavaScript are often used to determine whether a variable is empty or what type it is. But there are still differences between them:

typeof

##typeof is a unary operation, placed before an operand, Operands can be of any type.

The return value is a string that describes the type of the operand. Typeof generally can only return the following results:

number, boolean, string, function, object, undefined. We can 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 (undeclared), it will If an error occurs, using typeof for special objects such as Array and Null will always return object. This is the limitation of typeof.

instanceof

instance: instance, example


a instanceof b?alert("true"):alert(" false"); //a is an instance of b? True: False


instanceof is used to determine whether a variable is an instance of an object, such as 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 a subclass of object. Another example: function test(){};var a=new test();alert(a instanceof test) will return


Speaking of instanceof, we need to insert one more question, which is the arguments of the function. We all may think that arguments is an Array, but if you use instanceof to test, you will find that arguments are not an Array object, although it looks similar.


Also:


Test var a=new Array();if (a instanceof Object) alert('Y');else alert('N');


Get'Y'


But if (window instanceof Object) alert('Y');else alert('N');


Get'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)) will get object

The above is what I compiled for everyone, I hope it will be better in the future Helpful to everyone.

Related articles:

Explanation on the use of js operators single and vertical bars "|" and "||"

##JS Detailed explanation of full screen and exit full screen (including code)


The difference between ordinary functions and constructors in Javascript (detailed explanation combined with code)


The above is the detailed content of An in-depth explanation of the difference between typeof and instanceof in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!