typeof 操作符返回一个字符串,表示未经计算的操作数的类型。
使用方法:typeof(变量)或typeof 变量
// 返回类型包含"string","number","boolean","undefined","object","function","bigint","symbol"
1."string"
typeof 'asd'||String(8)||(typeof 8); // 注意typeof 返回的总是一个字符串
2."number"
typeof 123||NaN||Infinity||Math.random() // 注意NaN和Infinity和计算公式等都 返回的是"number"
3."boolean"
typeof true||Boolean(8)||!8||!!8 // 注意使用了!之后,不管是几个!,返回的都是"boolean"
4."undefined"
typeof undefined
5."object"
typeof {}||[]||null||new [String|Number|Array|Object等等]() // 注意null和{}和[]都返回"object",除 Function 外的所有构造函数的类型都是 'object'
6."function"
typeof function(){}||Math.random||class A{} // 注意方法函数不调用和es6中class类都是"function"
7."bigint"
typeof 8n // 注意"bigint"是ECMAScript 2020 新增类型
8."symbol"
typeof Symbol() // 注意"symbol"是ECMAScript 2015 新增