js中typeof类型检查及null和undefined区别

Dear 丶 2022-12-25 05:53 267阅读 0赞

typeof操作符

typeof操作符可进行类型检测

null —typeof null 返回object
undefined —typeof undefined 返回undefined
boolean —typeof true/false 返回boolean
number —typeof 数字 返回number
string – typeof ‘str’ 返回string
object —typeof obj(对象) 返回object
function typeof fun(函数) 返回function

** typeof检测null时,也会返回object,不能区分两者,typeof在检测未声明的变量时也会返回undefined**
undefined和null区别:
undefined是在使用var声明但未对变量进行初始化时,该值时undefined
null是一个空对象,如果定义的变量准备在将来用于保存对象,可初始化为null,如果想让对象被垃圾回收器回收,也可以设为null
undefined和null在使用(==)的情况下,为true,使用三个等于(=)的情况下为false

发表评论

表情:
评论列表 (有 0 条评论,267人围观)

还没有评论,来说两句吧...

相关阅读