In JavaScript there is a common
5 basic data types, respectively: number,string,boolean,null,undefined
7 Types of references are: Object type, array type, date type, regexp type, function type, basic wrapper type, monomer built-in object.
* Note the value (object) of the reference type is an instance of the reference type. In ECMAScript, it is used to combine data and functionality. It has long been a class, but it is not appropriate to call it "[1]
How do we judge the data type, and when we want to determine what data type a variable belongs to, JavaScript provides the way that we can judge it?
With the basic data type number and reference type array class example//Method 1:typeof
var b = 222; var c= [];alert (typeof B); // number alert (typeof C); // Object
View Code
Method 2 Instanceof The method is limited to reference type usage. and pay attention to capitalization.
1 var b = 222; 2 var c= [n/a]; 3 instanceof number); // Error 4 instanceof Array); // true 5 instanceof Array); // true
View Code
Method 3 Constructor
1 var b = 222; 2 var c= [n/a]; 3 alert (c.constructor = = = Array); // true 4 alert (B.constructor = = number); // True This should be JavaScript automatically converting variables to basic wrapper types
View Code
Method 4 prototype pay attention to case
1 var b = 222 2 var c= [All ]; 3 alert (Object.prototype.toString.call (b) = = = ' [Object number] '); // false 4 alert ( Object.prototype.toString.call (c) = = = ' [Object Array] '); // false 5 alert (Object.prototype.toString.call (b) = = = ' [Object Array] ') // true 6 alert ( Object.prototype.toString.call (c) = = = ' [object Array] ') // true
View Code
Reference:
[1] [High-performance JavaScript programming]. (High.Performance.JavaScript). Nicholas.c.zakas. Chinese-English version
Javasript data types and how to determine data types