参考自: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的更多相关文章

  1. js 中null,undefined区别

    首先摘自阮一峰先生的文章: 大多数计算机语言,有且仅有一个表示"无"的值,比如,C语言的NULL,Java语言的null,Python语言的None,Ruby语言的nil. 有点奇 ...

  2. js中 null, undefined, 0,空字符串,false,不全等比较

    null == undefined // true null == ''  // false null == 0 // false null == false // false undefined = ...

  3. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  4. JS中NULL和undifined区别及NULL的作用

    1.博客地址:http://www.cnblogs.com/eastday/archive/2010/03/03/1677324.html 2.参考地址2:https://www.zhihu.com/ ...

  5. js中的undefined与null、空值的比较

    最近在修改一个项目,总是报Js错误: 无法获取属性“length”的值: 对象为 null 或未定义 点开调试之后,惊奇的发现markerArr的值是undefined 所以我就将代码改成如下形式: ...

  6. 浅谈js中null和undefined的区别

    在JS中,null和undefined是经常让人摸不着头脑的东西,尤其是在数据初始化以及处理的过程中,经常稍微不注意,就会让页面在渲染时出现报错,下面来细说下,这两者之间的区别: null 表示一个对 ...

  7. JS中Null与Undefined的区别--2015-06-26

    在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...

  8. 区分JS中的undefined,null,"",0和false

    在程序语言中定义的各种各样的数据类型中,我们都会为其定义一个"空值"或"假值",比如对象类型的空值null,.NET Framework中数据库 字段的空值DB ...

  9. JS中null与undefined的区别

    1.typeof操作符 用来检测变量的数据类型 例:typeof 3.14 //返回number typeof [1,2,3]  //返回object 2.null 只有一个值的特殊类型,表示一个空对 ...

随机推荐

  1. 生成静态页html

    代码: using System; using System.Collections; using System.Collections.Generic; using System.IO; using ...

  2. 开发中常遇到的Python陷阱和注意点-乾颐堂

    最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...

  3. Mockplus 3.2前瞻,五大特色功能让你惊喜!

    在这个火热的夏季,我们有理由热切期待Mockplus 3.2的发布! 作为国产的一流原型设计工具,Mockplus 3.2版本会给我们带来什么呢? 格子(Repeater) 我们平常的设计,有大量需要 ...

  4. db2中如何获取当前日期前一周的日期

    SELECT CURRENT_DATE - (DAYOFWEEK(CURRENT_DATE) - 2 + (ROW_NUMBER() OVER (ORDER BY 1) ) ) DAY AS resu ...

  5. KbmMW 4.50.00 测试版发布

    We are happy to announce the release of kbmMW v. 4.50.00 Beta Professional and Enterprise Edition wi ...

  6. python的介绍和及基本的使用

    一 什么是计算机 1 计算机就是由一堆硬件组成的一个机器. 2 硬件的分类: CPU:犹如人类的大脑,运行着需要运行的程序. 内存:将 CPU要运行的内容从硬盘中读取出来,然后CPU在内存里拿内容,只 ...

  7. spring mvc 用cookie和拦截器实现自动登录(/免登录)

    Cookie/Session机制详解:http://blog.csdn.net/fangaoxin/article/details/6952954 SpringMVC记住密码功能:http://blo ...

  8. 2018.06.27Going Home(二分图匹配)

    Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24716 Accepted: 12383 Descript ...

  9. MySQL优化Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available

    //查看所有进程 show processlist; //查询是否锁表 show OPEN TABLES where In_use > 0; //查看被锁住的 SELECT * FROM INF ...

  10. vs2010点调试,显示系统找不到指定的文件

    首先,查看“项目”-“属性”-“链接器”-“常规”-“输出文件”,路劲是否是“bin/xxx.exe”,如果是请继续看我的解答,否则请忽略下面的内容.原因是用VS2010加载调试以前的VC6.0下的程 ...