参考自: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. dede后台一片空白

    原因是你修改了后台的数据库连接信息文件data/common.inc.php,保存的后文件编码并不是utf-8,而是变成了ANSI或utf-8 + bom的. 解决方法: 用editplus或note ...

  2. tp中url地址大小写问题

    在tp配置文件中有一个URL_CASE_INSENSITIVE选项,设置为true,表示大小写不敏感. 'URL_CASE_INSENSITIVE' => true

  3. AssemblyVersion,AssemblyFileVersion解释以及获取

    简而言之,AssemblyVersion: 是程序集的版本,.NET的CLR用,用于标识出该dll的版本信息,用于定义强名称的版本号: AssemblyFileVersion: 为编译器生成的文件加入 ...

  4. OCI 编程

    一.环境的配置 1.系统环境:要想使用OCI编程需要安装Oracle的客户端,而这个普通的客户端比较大,方便起见,可以安装即时客户端(Instantclient)作为Oracle的访问客户端.  具体 ...

  5. Luogu 3959 [NOIP2017] 宝藏- 状压dp

    题解 真的想不到这题状压的做法...听说还有跑的飞快的模拟退火,要是现场做绝对滚粗QAQ. 不考虑深度,先预处理出 $pt_{i, S}$ 表示让一个不属于 集合 $S$ 的 点$i$ 与点集 $S$ ...

  6. Java中通过SimpleDateFormat格式化当前时间:/** 输出格式:20060101010101001**/

    import java.util.*; import java.text.SimpleDateFormat; int y,m,d,h,mi,s,ms; String cur; Calendar cal ...

  7. Java 8 Stream API

    Java 8 Stream API JDK8 中有两大最为重要的改变.第一个是 Lambda 式:另外 Stream API(java.util.stream.*) Stream 是 JDK8 中处理 ...

  8. TLS/SSL简单过程

    .wcf的认证分为两种 1.1 transport模式,在传输层完成认证(只能在传输层完成认证,利用硬件加速效率更高) a.在使用transport模式,非windows凭证的情况下默认使用TLS/S ...

  9. DB2数据类型(抄袭)

    数字 数据类型        精度          最小值                                        最大值                       smal ...

  10. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...