JavaScript所有数据类型及判断方法
类型名称 | 实例 | 判断方法 |
---|---|---|
字符串 | “大家好” | typeof “大家好” == “string” //输出true |
数字 | 100 | typeof 100 == “number” //输出true |
布尔 | true | typeof true== “boolean” //输出true |
日期 | new Date() | new Date() instanceof Date //输出true |
数组 | [1,2,3] | Array.isArray( [1,2,3] ) //输出true |
JSON对象 | {“key”:“value”} | Object.prototype.toString.call(obj) === “[object Object]” //输出true |
set | new Set([1, 2, 3 ]) | Object.prototype.toString.call(new Set([1, 2, 3 ])) === “[object Set]” //输出true |
map | new Map() | Object.prototype.toString.call(new Map()) === “[object Map]” //输出true |
正则 | const reg = new RegExp(’(abc)*’) | Object.prototype.toString.call(reg)==="[object RegExp]" //输出true |
方法function | function test(a){} | typeof function test(a){} ==“function” //输出true |
null(不存在的对象) | null | null == null //输出true |
undefined(对象属性不存在/定义变量未赋值) | undefined | typeof undefined == “undefined” //输出true |
NaN(NaN属性是代表非数字值的特殊值) | NaN | isNaN(NaN) //输出true |