参考自: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. 【原创】Silverlight之TextBox的LostFocus、GotFocus事件

    <TextBox x:Name="txtCount" Width="200" Height="35" GotFocus="t ...

  2. js如何切割字符串

    <script language="javascript"> str="2,2,3,5,6,6"; //这是一字符串 var strs= new A ...

  3. Car 加油

    package com.hanqi; public class Car { //属性 成员变量 //车的颜色 String YanSe; //车的品牌 String PinPai; //邮箱容量 do ...

  4. conn not captured

    线程 和 事件中 变量 not captured 把变量定义为 static 或者添加为全局变量(放在main之前)

  5. Task构造

    //原文:http://www.tuicool.com/articles/IveiQbQ 创建并且初始化Task 使用lambda表达式创建Task Task.Factory.StartNew(() ...

  6. wpf 进度条 下拉

    <Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsof ...

  7. maven 无法下载私服jar包,如刚上传的第三方jar包无法下载。。

    原因可能是: 在你下载该文件时 ,的确 私服上没有该文件. 但是maven会在本地仓库建立文件夹路径,并且今天不会再去私服下载. 即使你现在上传3rd jar ,也不会去下载,导致一直找不到jar.. ...

  8. 乞丐版servlet容器第4篇

    6. NIOConnector 现在为Server添加NIOConnector,添加之前可以发现我们的代码其实是有问题的.比如现在的代码是无法让服务器支持同时监听多个端口和IP的,如同时监听 127. ...

  9. 2018.08.15 bzoj3747: [POI2015]Kinoman(线段树)

    传送门 简单题. 先不管时间复杂度看看怎么做. 对于一段区间[l,r],如果从右端加入一个数a[r+1],对这个区间有什么影响?显然如果区间中已经有了a[r+1]这个数就会产生-a[i+1]的影响,否 ...

  10. 一组RS485设备操作命令

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ZNJM ...