Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What are the methods for JS to judge the data type?
What are the methods for JS to judge the data type?
there are several ways for js to judge data types: typeof method is used to return the string form of the type, instanceof method is used to judge the prototype, constructor method and Object.prototype.toString method

There are many methods in JavaScript that can help us to judge data types. Next, we will introduce these methods in detail in the article, which has certain reference function and hopes to help everyone.

recommended course: JavaScript tutorial

method 1: typeof method

typeof is an operator, with a unary expression on the right, and returns the data type of this expression. The returned result is expressed in the form of this type of string, including: number, boolean, symbol, string, object, undefined, function, etc.

the return value is divided into the following types:

for the basic type. Except for the null value, which returns the object, everything else returns the correct result

For the reference value, except the function type, everything else returns the object type

Example:

<; script type="text/javascript">

var a = "string";

console.log(a); //string

var a = 1;

console.log(typeof a); //number

var a = false;

console.log(typeof a); //boolean

var a;

console.log(typeof a); //undfined

var a = null;

console.log(typeof a); //object

var a = document;

console.log(typeof a); //object

var a = [];

console.log(a); //[]

var a = function() {};

console.log(typeof a) //function

< /script> Rendering:

Method 2: instanceof Method

instanceof is used to judge whether A is an instance of B, and the expression is: A instanceof B, if A is an instance of B, it will return true, otherwise it will return false. It is important to note here that instanceof detects the prototype.

< script type="text/javascript">

var a=[];

console.log(a instanceof Array)

< /script> Results returned: true

Method 3: constructor method

When a function is defined, the JS engine will add a prototype for it, and then add a constructor attribute on the prototype to point to the reference of the function.

when var f = new F () is executed, f is regarded as a constructor, and f is an instance object of f, and the constructor on the prototype of f is passed to F., Therefore, f.constructor == F

method 4: Object.prototype.toString method

toString is a method on the Object prototype object, which returns the specific type of its caller by default, more strictly speaking, it is the object type that this points to when toString runs, and the returned type format is [object, Xxx],xxx is a specific data type, including: String, Number, Boolean, Undefined, Null, Function, Date, Array, RegExp, Error, HTMLDocument and so on, which can be obtained by this method.

console.log(Object.prototype.toString.call(num));

console.log(Object.prototype.toString.call('')) ;

console.log(Object.prototype.toString.call(1)) ;

console.log(Object.prototype.toString.call(true)) ;

console.log(Object.prototype.toString.call(null)) ;

console.log(Object.prototype.toString.call(new Function()) );

console.log(Object.prototype.toString.call(new Date())) ;

console.log(Object.prototype.toString.call([])) ;

console.log(Object.prototype.toString.call(document)) ;

console.log(Object.prototype.toString.call(window) ); Renderings:

Summary: