以及对象(Object)
typeof true; // "boolean"
typeof null; // "object" (历史遗留问题,实际应为“null”)
typeof undefined; // "undefined"
typeof 42; // "number"
typeof BigInt(9007199254740991); // "bigint"
typeof BigInt(1n) === 'bigint'; // true
typeof "Howdy"; // "string"
let sym = Symbol("description");
typeof sym; // 返回 "symbol"
可以更准确地判断包括 null 和数组、函数等在内的复杂类型
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(42); // "[object Number]"
Object.prototype.toString.call(BigInt(9007199254740991)); // "[object BigInt]"
Object.prototype.toString.call("Howdy"); // "[object String]"
Object.prototype.toString.call({}) === '[object Object]';
Object.prototype.toString.call([]) === '[object Array]';
Object.prototype.toString.call(new Date()) === '[object Date]';
判断一个对象是否是某个构造函数的实例,对于引用类型非常有用,但对基本类型(如布尔值、数字、字符串、null、undefined 和 BigInt)无效。
// 对于引用类型
let arr = [];
arr instanceof Array; // true
Array.isArray([]); // true
// or
Object.prototype.toString.call([]) === '[object Array]';