跟我学JavaScript--typeof,null,undefined

typeof操作符

作用:检测变量的数据类型

例.

typeof "Nico"                 //string
typeof 21                     //number
typeof true                   //boolean
typeof [1,2,3]                //object
typeof [name:"Nico",age:21]   //object

注:JavaScript中数组是一种特殊的对象类型

null

null是一个只有一个值的特殊类型

typeof null  //object

null表示一个空对象的引用

可用于清空对象
如.

var person = null;   //值为null,类型为object

undefined

undefined是一个没有设置值的变量

typeof undefined  //undefined

没有定义值的变量的类型为undefined

var person;       //值为undefined
typeof person;    //类型也为undefined

任何变量都可以通过设置值为undefined来清空
如.

var person={Name:"Nico",age:21}
person = undefined;    //值为undefined
typeof person;         //类型也为undefined

undefined和null的区别:

typeof undefined             // undefined
typeof null                  // object
null === undefined           // false
null == undefined            // true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值