The ECMAScript standard defines 7 data types: Boolean, Null, Undefined, number, String, Symbol (ES6 new), and object, and 6 data types other than object are also known as basic data types. In addition, there are complex data types such as array, function, and so on. This paper introduces the general type judgment method, and finally gives a comprehensive data type judgment method.
First, typeof
typeof is a unary operator (not a function method) that can identify basic data types other than NULL, as well as object and function. Its return value is a lowercase string:
/**** typeof ****/typeof Notoginseng; Output "number" typeof "AAA"; Output "string" typeof undefined; Output "undefined" typeof false; Output "Boolean" typeof {A:1,b:2}; Output "Object" typeof function () {}; Output "function"//it cannot discriminate null and arraytypeof null; Output "Object" typeof [+]; Output "Object"
Second, Object.prototype.toString.call ()
Perfect for identifying basic data types and types such as Object, Function, Array, Date, math, and more
/**** Object.prototype.toString.call ****/object.prototype.tostring.call ("AA"); Output "[Object String]" Object.prototype.toString.call (123); Output "[Object number]" Object.prototype.toString.call (null); Output "[Object Null]" Object.prototype.toString.call (undefined); Output "[Object Undefined]" Object.prototype.toString.call ([+)]; Output "[Object Array]" Object.prototype.toString.call (function () {}); Output "[Object Function]" Object.prototype.toString.call ({a:1,b:2}); Output "[Object Object]"
Iii. Other methods of judging
Array.isarray () can determine whether a data is an array type.
The instanceof operator is used to test whether an object has a property in its prototype chain that has a constructor prototype
. In some cases it can also be used to detect data types, using caution.
/**** IsArray judgment Array ****/var arrtmp = [up]; Array.isarray (arrtmp); Output true/**** instanceof judgment type, inaccurate ****/var numTmp1 = 123;var NUMTMP2 = new number (123); numTmp1 instanceof number; Output FALSENUMTMP2 instanceof number; Output truearrtmp instanceof Array; Output truearrtmp instanceof Object; Output true
Iv. full set of judgment methods
(The following code moved from Anguar source, a little trimming)
var toString = object.prototype.tostring;function isundefined (value) {return typeof value = = = ' undefined ';} function isDefined (value) {return typeof value!== ' undefined ';} function IsObject (value) {return value!== null && typeof value = = = ' object ';} function Isstring (value) {return typeof value = = = ' String ';} function Isnumber (value) {return typeof value = = = ' number ';} function IsDate (value) {return Tostring.call (value) = = = ' [Object Date] ';} var IsArray = array.isarray;function Isfunction (value) {return typeof value = = = ' function ';} function Isregexp (value) {return Tostring.call (value) = = = ' [Object RegExp] ';} function IsWindow (obj) {return obj && Obj.window = = = obj;} function Isfile (obj) {return Tostring.call (obj) = = = ' [Object File] ';} function Isformdata (obj) {return Tostring.call (obj) = = = ' [Object FormData] ';} function Isboolean (value) {return typeof value = = = ' Boolean ';} function Ispromiselike (obj) {return obj && isfuNction (Obj.then);} function iselement (node) {return!! (Node && (Node.nodename | | (Node.prop && node.attr && node.find)));} function Isarraylike (obj) {if (obj = = NULL | | IsWindow (obj)) {return false; } var length = "Length" in Object (obj) && obj.length; if (Obj.nodetype = = = 1 && length) {return true; } return isstring (obj) | | IsArray (obj) | | Length = = 0 | | typeof length = = ' number ' && length > 0 && (length-1) in obj;}
A complete set of methods for identifying JS data types