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”} | 1.通过正则判断 2. typeof {“key”:”value”} == “object” //输出true 提示:2方法在有时间/数组/null类型的情况下不准确 |
方法 | function test(a){} | typeof function test(a){} ==”function” //输出true |
null(不存在的对象) | null | null == null //输出true “大家好” == null //输出false |
undefined(对象属性不存在/定义变量未赋值) | undefined | typeof undefined == “undefined” //输出true |
NaN(NaN属性是代表非数字值的特殊值) | NaN | isNaN(NaN) //输出true |