了解undefined、null、NaN的区别
1.常规的解释,null是个对象,表示空值,undefined也是个对象,表示没有定义
2.详细分析
null
书上的解释(Javascript权威指南),Javascript的关键词null是一种特殊的值,它表示“无值”。null常常被看作对象类型的一个特殊值,即代表“无对象”的值。如果一个变量的值为null,那么你就会知道它的值不是有效的对象、数组、数字、字符串和布尔值。null对应类型object,布尔值false,数字0,字符串“null”
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
var test = null;//类型,输出objectdocument.write( typeof (test));document.write("<br/>");//字符串,输出nulltestdocument.write(test + 'test');document.write("<br/>");//数字,输出10document.write(test + 10);document.write("<br/>");//布尔值,输出falseif (test) { document.write("true");}if (!test) { document.write("false");} |
什么情况下会返回null
document.getElementById(‘XXX’); 寻找一个不存在的元素,返回null
undefined
undefined是window对象的一个属性,且不是关键词。书上解释,当你使用了一个并未声明的变量时,或者使用了已经声明但还没有赋值的变量时,又或者使用了一个并不存在的对象属性时,返回的就是这个值。undefined对应类型undefined,布尔型false,字符串undefined,数字计算结果一定是NaN
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
var test; //类型,输出undefineddocument.write( typeof (test));document.write("<br/>");//字符串,输出undefinedtestdocument.write(test + 'test');document.write("<br/>");//数字,输出NaNdocument.write(test + 10);document.write("<br/>");//布尔值,输出falseif (test) { document.write("true");}if (!test) { document.write("false");} |
什么情况下会返回undefined,有人总结了如下几种场景
1. 直接访问没有修改的全局变量undefined,var x = undefined 那x的值为undefined
2. 使用没有声明的变量,在IE下出错,提示”xxx”未定义;已经声明,没有赋值,类似var x; alert(x); 弹出undefined
3. 使用了一个不存在的对象的属性
|
1
2
3
4
5
|
var coffee = { x:'1', y:2}alert(coffee.z); |
上面有提到过undefined不是javascript的关键词,所以可以定义一个名字为undefined的变量,如下
|
1
2
3
4
5
|
var undefined = 10;document.write( typeof (undefined));document.write("<br/>")document.write(undefined * 10); |
在IE和搜狗浏览器下,返回结果 number 100
在firefox和chrome下,返回结果 undefined NaN
所以最好不要使用undefined的变量名
使用undefined的一个场景
在某个场景中经常要用到undefined,最好定义一个变量类似
var x = undefined,可以提高性能。这是因为javascript引擎在使用undefined的时候都是遍历window对象,寻找undefined属性,遍历属性过程中会造成大量的时间。
null和undefined的比较
null == undefined 返回true
null===undefined 返回false
对 undefined 的优化
当我们在程序中使用 undefined 时,实际使用的是 window 对象的 undefined 属性,由于 window 对象的属性很多,在每一次与 undefined交时,搜索 window 对象的 undefined 属性都会花费时间。在需要经常使用 undefined 的场景中,可以定义一个局部的 undefined 变量。
例如:var undefined;
最后引用淘宝玉伯对null和undefined的解释
值类型的“虚无”用undefined,引用类型的“虚无”,用null。
了解undefined、null、NaN的区别的更多相关文章
- js判断undefined类型,undefined,null,NaN的区别
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined 所以自作聪明判断 ...
- Javascript 中的非空判断 undefined,null, NaN的区别
JS 数据类型 在介绍这三个之间的差别之前, 先来看一下JS 的数据类型. 在 Java ,C这样的语言中, 使用一个变量之前,需要先定义这个变量并指定它的数据类型,是整型,字符串型,.... 但是 ...
- (转载)Javascript 中的非空判断 undefined,null, NaN的区别
原文地址:https://blog.csdn.net/oscar999/article/details/9353713 在介绍这三个之间的差别之前, 先来看一下JS 的数据类型. 在 Java ,C ...
- JavaScript 中undefined,null,NaN的区别
1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型.var a1;var a2 = true;va ...
- js中undefined,null,NaN的区别
1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型.var a1;var a2 = true;va ...
- null、undefined和NaN的区别
未定义的值和定义未赋值的值是undefined: null是一种特殊的Object,可以给变量赋一个值null,来清除变量的值: NaN是一种特殊的number:
- undefined和NAN的区别(转)
Javascript 中 null.NaN和undefined的区别 1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始 ...
- js判断undefined类型,undefined,null, 的区别详细解析
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...
- 【转】javascript中not defined、undefined、null以及NaN的区别
原文链接(点击跳转) 第一:not defined 演示代码: <span style="font-size:12px;"><span style=" ...
- 字符串怎么换行 || 字符串中使用单引号时应该怎么写 || 保留两位小数 || 数字0在if中的意思是false || 什么情况下会会报undefined || null和undefined的区别 ||
换行的字符串 "This string\nhas two lines" 字符串中使用单引号时应该怎么写 'You\'re right, it can\'t be a quote' ...
随机推荐
- [LOJ 1248] Dice (III)
G - Dice (III) Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Descri ...
- UVA 1324 The Largest Clique 最大团(强连通分量,变形)
题意:给一个有向图,要求找出一些点,使得这些点中的任意点对,要么可以互通,要么单向可达. 思路:最低只要求单向可达即可,即a->b都可以算进去. 强连通分量内的点肯定是满足要求的,可以全选,但是 ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- Android 隐藏RadoiButton左边按钮
声明方式 添加属性 android:button=“@null”? 代码方式 radioBtn.setButtonDrawable(new StateListDrawable());
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.10
Every $k\times k$ positive matrix $A=(a_{ij})$ can be realised as a Gram matrix, i.e., vectors $x_j$ ...
- 浏览器插件 - 通用注入模版JS
//TIP:先通过Tampermonkey编写为可用脚本,再套用此通用模版,再拖到Chrome安装为扩展即可. /* 通用注入原型3:*/ switch (window.location.pathna ...
- msp时钟设置程序
吐槽一下MSP430需要明白的东西: 在430中,一个时钟周期 = MCLK晶振的倒数.如果MCLK是8M,则一个时钟周期为1/8us: 一个机器周期 = 一个时钟周期,即430每个动作都能完成一个基 ...
- Javascript/15-1-14
1.break 语句用于跳出循环.break 语句跳出循环后,会继续执行该循环之后的代码(如果有的话). continue 用于跳过循环中的一个迭代. 2.label:statements 用于直接跳 ...
- 关于T公司的强矩阵架构的思考
我所在的T公司是强矩阵架构,关于这类公司,应该是不少大公司的主流架构,也就是说一个职员在公司内不仅在项目内有相应的级别,在其行政上也是有相应的级别,日常工作以项目的内容为主,但是同时也是属于行政的一员 ...
- 关于图像读取函数imread()的一点使用经验,注意默认参数的赋值
读入数字图像到数组,用CNN进行训练,发现关于图像读取的一个问题. 问题描述:读取灰度数字图像,在验证时发现存在错误,从图像到数组中的值不完全一样? main code as follows: int ...