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 只有一个值的特殊类型,表示一个空对 ...
随机推荐
- Python threading 单线程 timer重复调用函数
项目中需要使用定时器,每次都使用构造器函数调用: timer = threading.Timer(timerFlag, upload_position) timer.start() 打印线程后发现,每 ...
- OC 里面 webView与js
webView与js的交互流程吗,iOS端暴露函数 ,js直接调用 [链接]WKWebView-如何通过JS调用OC方法 https://www.jianshu.com/p/68f799d6679e ...
- Golang之http编程
Go原生支持http.import("net/http") Go的http服务性能和nginx比较接近 几行代码就可以实现一个web服务 服务端http package main ...
- winXP使用
1.获得管理员权限 开机启动时按F8-->进入“安全模式”-->选择“Administrator”-->点击登录 2.Windows XP属于单用户多任务操作系统,Linux属于多用 ...
- [NOI.AC]COUNT(数学)
解析: 也可以将所有的可能都计算出来,后进行减法运算. 代码: #include<bits/stdc++.h> using namespace std; #define ll long l ...
- [SoapUI] 各种日期计算
import java.util.*; import java.text.SimpleDateFormat; // current date String dateNew = today() // t ...
- 模型参数_grid
from sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn.preproc ...
- 2018.10.15 NOIP训练 hyc的等比数列(数论+枚举)
传送门 一道不错的枚举题. 显然桶排序之后瞎枚举一波. 考虑枚举首项和末项,假设首项除去一个最大的平方因子得到的结果为xxx. 那么末项一定等于xxx乘上一个平方数. 于是我们枚举首项,算出xxx然后 ...
- 2018.09.07 codeforces311B. Cats Transport(斜率优化dp)
传送门 斜率优化dp好题. 对于第i只猫,显然如果管理员想从出发开始刚好接到它,需要在t[i]=h[i]−dist(1,i)" role="presentation" s ...
- CentOS7查看网络的相关命令
转载自:https://www.linuxidc.com/Linux/2015-07/119555.htm 1.切换到超级用户 [Oscar@localhost 桌面]$ su root 2.查询可用 ...