js中null, undefined 和 typeof
参考自:http://www.cnblogs.com/wicub/p/3442891.html
typeof 是运算符,注意不是函数,是运算符,其作用,是考察变量究竟是什么类型。或曰,是变量是否定义或是否初始化的照妖镜。返回值是字符串。
undefined 表示一个对象没有被定义或者没有被初始化。
null 表示一个尚未存在的对象的占位符。
首先做四个测试:
//测试1: 变量没有定义时,只能使用typeof
//console.log('a == undefined: ' + a == undefined); //报错
//console.log('a == null: ' + a == null); //报错
//console.log('a === undefined: ' + a === undefined); //报错
//console.log('a === null: '+ a===null); //报错
console.log('typeof a == undefined: ' + (typeof a == undefined)); //false
console.log('typeof a == \'undefined\': ' + (typeof a == 'undefined')); //true
console.log('typeof a === \'undefined\': ' + (typeof a === 'undefined')); //true
console.log(typeof a); //undefined
//测试2:变量有定义,但未初始化,typeof,undefined,null都可以使用
var b;
console.log('b == undefined: ' + (b == undefined)); //true
console.log('b == null: ' + (b == null)); //true
console.log('b === undefined: ' + (b === undefined)); //true
console.log('b === \'undefined\': ' + (b === 'undefined')); //false
console.log('b === null: '+ (b===null)); //false
console.log('typeof b == undefined: ' + (typeof b == undefined)); //false
console.log('typeof b == \'undefined\': ' + (typeof b == 'undefined')); //true
console.log('typeof b === \'undefined\': ' + (typeof b === 'undefined')); //true
console.log(typeof b); //undefined
//测试3:变量有定义且已经初始化
b = 0;
console.log('b == undefined: ' + (b == undefined)); //false
console.log('b == null: ' + (b == null)); //false
console.log('b === undefined: ' + (b === undefined)); //false
console.log('b === \'undefined\': ' + (b === 'undefined')); //false
console.log('b === null: '+ (b===null)); //false
console.log('typeof b == undefined: ' + (typeof b == undefined)); //false
console.log('typeof b == \'undefined\': ' + (typeof b == 'undefined')); //false
console.log('typeof b === \'undefined\': ' + (typeof b === 'undefined')); //false
console.log(typeof b); //number
//测试4: 变量是函数参数
function test(b){
console.log('b == undefined: ' + (b == undefined)); //true
console.log('b == null: ' + (b == null)); //true
console.log('b === undefined: ' + (b === undefined)); //true
console.log('b === \'undefined\': ' + (b === 'undefined')); //false
console.log('b === null: '+ (b===null)); //false
console.log('typeof b == undefined: ' + (typeof b == undefined)); //false
console.log('typeof b == \'undefined\': ' + (typeof b == 'undefined')); //true
console.log('typeof b === \'undefined\': ' + (typeof b === 'undefined')); //true
console.log(typeof b); //undefined
}
test();
null和undefined的设计初衷:
1. undefined:表示一个对象没有被定义或者没有被初始化。
2. null:表示一个尚未存在的对象的占位符。
undefined和null是相等的。有:
console.log(undefined == null); //true
console.log(undefined === null); //false
未声明的对象只能用typeof运算符来判断!!否则会报错
console.log(undefined == null); //true
console.log(undefined === null); //false console.log(typeof undefined); //undefined
console.log(typeof null); //object
console.log(typeof "string"); //string
console.log(typeof 0); //number
console.log(typeof function(){}); //function
console.log(typeof true); //boolean
console.log(typeof {}); //object console.log(typeof null == 'null'); //false null类型返回object,这其实是JavaScript最初实现的一个错误,然后被ECMAScript沿用 了,也就成为了现在的标准。所以需要将null类型理解为“对象的占位符”,就可以解释这一矛盾,虽然这只是一中 “辩解”。对于代码编写者一定要时刻警惕这个“语言特性”
js中null, undefined 和 typeof的更多相关文章
- js 中null,undefined区别
首先摘自阮一峰先生的文章: 大多数计算机语言,有且仅有一个表示"无"的值,比如,C语言的NULL,Java语言的null,Python语言的None,Ruby语言的nil. 有点奇 ...
- js中 null, undefined, 0,空字符串,false,不全等比较
null == undefined // true null == '' // false null == 0 // false null == false // false undefined = ...
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
浅谈JS中的!=.== .!==.===的用法和区别 var num = 1; var str = '1'; var test = 1; test == num //tr ...
- JS中NULL和undifined区别及NULL的作用
1.博客地址:http://www.cnblogs.com/eastday/archive/2010/03/03/1677324.html 2.参考地址2:https://www.zhihu.com/ ...
- js中的undefined与null、空值的比较
最近在修改一个项目,总是报Js错误: 无法获取属性“length”的值: 对象为 null 或未定义 点开调试之后,惊奇的发现markerArr的值是undefined 所以我就将代码改成如下形式: ...
- 浅谈js中null和undefined的区别
在JS中,null和undefined是经常让人摸不着头脑的东西,尤其是在数据初始化以及处理的过程中,经常稍微不注意,就会让页面在渲染时出现报错,下面来细说下,这两者之间的区别: null 表示一个对 ...
- JS中Null与Undefined的区别--2015-06-26
在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...
- 区分JS中的undefined,null,"",0和false
在程序语言中定义的各种各样的数据类型中,我们都会为其定义一个"空值"或"假值",比如对象类型的空值null,.NET Framework中数据库 字段的空值DB ...
- JS中null与undefined的区别
1.typeof操作符 用来检测变量的数据类型 例:typeof 3.14 //返回number typeof [1,2,3] //返回object 2.null 只有一个值的特殊类型,表示一个空对 ...
随机推荐
- Oracle完全卸载
停用oracle服务:进入计算机管理,在服务中,找到oracle开头的所有服务,右击选择停止 在开始菜单中,找到Universal Installer,运行Oracle Universal I ...
- .net VS2008 时间加减,时间段,时间格式化到秒
举个例子: DateTime time1 = DateTime.Now; DateTime time2 = time1.AddDays(1); time1是当前时间,time2比当前时间多一天.也就是 ...
- Docker常见问题
问题 当我使用docke search mysql时,显示如下错误: [root@iZ25u61v97hZ opt]# docker search redis Segmentation Fault o ...
- CentOS设置yum存储库 (nginx)
要为RHEL / CentOS设置yum存储库,请创建/etc/yum.repos.d/nginx.repo 使用以下内容命名的文件 : [nginx] name=nginx repo baseurl ...
- 无法更新 EntitySet 因为它有一个 DefiningQuery
DbFirst 使用 MVC+EF+仓储+ADO.NET实体数据模型 无法更新 EntitySet“Book”,因为它有一个 DefiningQuery definingqueryentityfram ...
- JTemplate学习(四)
注释.自定方法.模板嵌套子模板.循环输出不同class <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "htt ...
- loadrunner12-运行报错原因及解决办法整理集合
1.错误:已超过该load generator的CPU使用率80%: 答:机器内存过小,更换配置更好的机器来执行测试. 是因为虚机的内存过小,运行Controller需要消耗的CPU过高,超过了80% ...
- 16进制string转成int
http://blog.csdn.net/wl1524520/article/details/25706521
- cocos sdkbox android 接入注意
1 jin/Android.mk 中 LOCAL_SRC_FILES 链接的是runtime-src/Classes中的cpp文件 2 jin/Android.mk 若 LOCAL_LDLIBS ...
- Devexpress VCL Build v2014 vol 14.2.4 发布
What's New in 14.2.4 (VCL Product Line) New Major Features in 14.2 What's New in VCL Products 14.2 ...