js中undefined、null、“”的认识

来源:互联网 发布:cydia不能安装软件 编辑:程序博客网 时间:2024/06/07 00:57
1.当声明的变量还未被初始化时,变量的默认值为undefined;
2.null用来表示尚未存在的对象,常用来表示函数试图返回一个不存在的对象;
js示例:
var ob;
alert(ob==undefined);//true
alert(nulll==document.getElementById('notExistElement'));// 当页面上不存在id为"notExistElement"的DOM节点时,这段代码显示为true
alert(typeof undefined);//undefined
alert(typeof null);//object,可以理解为null是一个不存在的对象的占位符
alert(null==undefined);//true,认为undefined是null派生出来的,所以把它们定义为相等
在某些情况下区分他们:
alert(null===undefined);//false
alert(typeof null==typeof undefined);//false
alert(!0); //true
alert(!false); //true
alert(!undefined); //true
alert(!null); //true
alert(!''); //true

==和===的区别
1、对于string,number等基础类型,==和===是有区别的
1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等
2)同类型比较,直接进行“值”比较,两者结果一样
2、对于Array,Object等高级类型,==和===是没有区别的 ,进行“指针地址”比较
3、基础类型与高级类型,==和===是有区别的
1)对于==,将高级转化为基础类型,进行“值”比较
2)因为类型不同,===结果为false
0 0