JavaScript checks the type of the variable and determines whether it is an integer or a string or other types, etc.
]
2. toString is originally used It is used for string conversion, but now it is popular for checking variable types. Shunzi also wrote a function here to facilitate checking the type of variables, which can be used instead of typeof
The code is as follows:
function getType(o) {
var _t; return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype. toString.call(o).slice(8,-1):_t).toLowerCase();
}
Execution result:
getType(" abc"); //string
getType(true); //boolean
getType(123); //number
getType([]); //array
getType({}); //object
getType(function(){}); //function
getType(new Date); //date
getType(new RegExp); //regexp
getType(Math); //math getType(null); //null