ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## typeof 区别 `number, string, object, boolean, function, undefined, symbol `这七种类型 ``` typeof 123 // "number" typeof '123' // "string" typeof false // "boolean" typeof undefined typeof window // "object" typeof {} // "object" typeof [] // "object" typeof null // "object" 历史原因 ``` ## instanceof 判断是否为实例 instanceof 是用来判断 A 是否为 B 的实例,**instanceof 检测的是原** ``` [] instanceof Array; // true {} instanceof Object;// true [] instanceof Object; // true new Date() instanceof Date;// true function Person(){}; new Person() instanceof Person; ``` ### 判断为声明的js变量 ``` // 错误的写法 if (v) { // ... } // 报错: v is not defined // 正确的写法 if (typeof v === "undefined") { // ... } ```