一、typeof
typeof 操作符的唯一目的就是检查数据类型,我们可以利用 typeof 来判断number, string, boolean, function, undefined, symbol
var str="string";
console.log(typeof str); //string
var num=1;
console.log(typeof num); //number
var bn=false;
console.log(typeof bn); //boolean
var a;
console.log(typeof a); //undfined
var bigint = BigInt("9007199254740995");
console.log(typeof bigint); //bigint
var syboml = Symbol("syboml");
console.log(typeof syboml); //syboml
var fn = function(){};
console.log(typeof fn); //function
如果我们希望检查任何从 Object 派生出来的结构类型,使用 typeof 是不起作用的,都会是"object"。检查 Object 种类的合适方式是使用 instanceof 关键字。但即使这样也存在误差。后面instanceof部分进行叙述。
var obj = null;
console.log(typeof obj); //object
var doc = document;
console.log(typeof doc);//object
var arr = [];
console.log(typeof arr); //object
var map = new Map();
console