in Web front-end development, we often need to judge the data type of a variable. Since ECMAScript is loosely typed, there is a need to have a means to detect the data type of a given variable--typeof is the operator responsible for providing this convenient information.
Using the typeof operator on a value may return one of the following strings: "Undefined"-if the value is not defined "boolean"-if the value is a Boolean "string"-if the value is a string "number"-if the value is a numeric " Object "-If this is an objects or null" function "-if this value is a function The return value of a commonly used typeof operator includes number, String, Boolean, Undefined, Object and function. such as: code as follows: Var n; Console.log (typeof N); "Undefined" n = 1; Console.log (typeof N); "Number" n = "1"; Console.log (typeof N); "string" n = false; Console.log (typeof N); "Boolean" N = {name: "obj"}; Console.log (typeof N); "Object" N = new number (5); Console.log (typeof N); "Object" N = function () {return;}; Console.log (typeof N); The "function" examples show that the operands of the typeof operator can be either a variable (message) or a literal number. Note that TypeOf is an operator rather than a function, so the parentheses in the example are not required (although they can be used). From the above example, we found that numbers created with number () are also returned with the value "Object" by TypeOf, because constructors return objects, so if we want to distinguish between numeric objects (number), What if you have a String object (string), an array object (array), a function object, a Day object (date), a Boolean object (Boolean), and a JavaScript built-in object such as an Error object (Error)? Here you can call the object's ToString method, such as: code as follows: VarN, Res; N = new number (66); res = Object.prototype.toString.call (n); Console.log (RES); "[Object number]" n = new String ("string"); res = Object.prototype.toString.call (n); Console.log (RES); "[Object String]" n = []; res = Object.prototype.toString.call (n); Console.log (RES); [Object Array] //...