概览
最新的 ECMAScript 标准定义了 9 种数据类型:
- 6 种原始类型,使用 typeof 运算符检查:
- undefined:
typeof instance === "undefined"
- Boolean:
typeof instance ==="boolean"
- Number:
typeof instance === "number"
- String:
ypeof instance === "string"
- BigInt:
typeof instance === "bigint"
- Symbol :
typeof instance === "symbol"
- undefined:
- null:
typeof instance === "object"
- Object:
typeof instance === "object"
。任何 constructed 对象实例的特殊非数据结构类型,也用做数据结构:new Object
,new Array
,new Map
,new Set
,new WeakMap
,new WeakSet
,new Date
,和几乎所有通过 new keyword 创建的东西。 - Function:非数据结构,尽管 typeof 操作的结果是:
typeof instance === "function"
。这个结果是为 Function 的一个特殊缩写,尽管每个 Function 构造器都由 Object 构造器派生。
typeof 操作符
检测给定变量的数据类型
var message = "ABBE";
alert(typeof message);
alert(typeof(message));
alert(typeof 95);