typeof
typeof 操作符返回一个字符串,表示未经计算的操作数的类型。(MDN)
typeof 可以用来检测一个值的类型。
1. 表现
在 ES6 之前,typeof 在浏览器的表现是这样的:
类型 | 结果 |
---|---|
Boolean | “boolean” |
String | “string” |
Number | “Number” |
Function | “function” |
undefined | “undefined” |
null | “object” |
数组 | “object” |
任意对象 | “object” |
typeof 233; // 输出:"number"
typeof '嘎?'; // 输出:"string"
typeof true; // 输出:"boolean"
typeof undefined; // 输出:"undefined"
var fn1 = function() {};
function fn2() {};
typeof fn1; // 输出:"function"
typeof fn2; // 输出:"function"
typeof null; // 输出:"object"
typ