Home > Web Front-end > JS Tutorial > What are typeof and instanceof in js? What's the difference?

What are typeof and instanceof in js? What's the difference?

藏色散人
Release: 2018-10-13 16:35:06
Original
6762 people have browsed it

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")}
Copy after login

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的实例?真:假
Copy after login
 var a=new Array();alert(a instanceof Array);
Copy after login

will return true, and

alert(a instanceof Object)
Copy after login

will also return true;

This is because Array is an object subcategory.

Another example:

function test(){};var a=new test();alert(a instanceof test)
Copy after login

will return object.

Test:

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

got 'Y', and

if (window instanceof Object) alert('Y');else alert('N');
Copy after login

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))
Copy after login

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!

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