Use the Typeof method to detect data types

1. The method is not precise, and the array and object are both object types in detection. 2. The result of the test is a string of that data type

// Use typeof to check data type console.log(typeof (123)); //number console.log(typeof ("123")); //string console.log(typeof (true)); //boolean console.log(typeof ([])); //object console.log(typeof ({})); //object console.log(typeof (null)); //object console.log(typeof (function(){})); //function console.log(typeof (undefined)); //undefinedCopy the code

Universal data type detection

Here’s a thought problem to plug into this knowledge

Question: Why is the result of an array call toString different from that of an object call toString

ToString = Array; toString = Array; toString = Array; If Array when the Array is calling toString access prototype toString / * Array of prototype. The toString () : the underlying Array call join () Object. The prototype. The toString () : Return fixed string [object datatype] */ console.log([1,2,3].tostring ()); / / 1, 2, 3 [1, 2, 3]. The join () the console. The log ({name: 'Joe'}. The toString ()); //[object Object] console.log([]); console.log([].toString()); //'' console.log({}.toString()); //[object object] //2. Since the Object. The prototype. The toString () will return a fixed format of the data type string, only need through the call to modify this point, you can detect all of the data type * / / / typeof keyword testing null and array will get Object, Unable to detect the console. The log (Object. The prototype. ToString. Call (' 123 ')); //[object String] console.log(Object.prototype.toString.call(123)); //[object Number] console.log(Object.prototype.toString.call(true)); //[object Boolean] console.log(Object.prototype.toString.call(undefined)); //[object Undefined] console.log(Object.prototype.toString.call(null)); //[object Null] console.log(Object.prototype.toString.call(function(){})); //[object Function] console.log(Object.prototype.toString.call([])); //[object Array] console.log(Object.prototype.toString.call({})); //[object Object]Copy the code