js判断undefined类型,undefined,null, 的区别详细解析
时间:2024-05-09 10:34:26
js判断undefined类型
今天使用showModalDialog打开页面,返回值时。当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined
所以自作聪明判断
var reValue=window.showModalDialog("","","");
if (reValue== undefined){
alert("undefined");
}
发现判断不出来,最后查了下资料要用typeof
方法:
if (typeof(reValue) == "undefined") {
alert("undefined");
}
typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"
js中undefined,null,NaN的区别
1.类型分析:
js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型。
var a1;
var a2 = true;
var a3 = 1;
var a4 = "Hello";
var a5 = new Object();
var a6 = null;
var a7 = NaN;
var a8 = undefined;
alert(typeof a); //显示"undefined"
alert(typeof a1); //显示"undefined"
alert(typeof a2); //显示"boolean"
alert(typeof a3); //显示"number"
alert(typeof a4); //显示"string"
alert(typeof a5); //显示"object"
alert(typeof a6); //显示"object"
alert(typeof a7); //显示"number"
alert(typeof a8); //显示"undefined"
从上面的代码中可以看出未定义的值和定义未赋值的为undefined,null是一种特殊的object,NaN是一种特殊的number。
2.比较运算
var a1; //a1的值为undefined
var a2 = null;
var a3 = NaN;
alert(a1 == a2); //显示"true"
alert(a1 != a2); //显示"false"
alert(a1 == a3); //显示"false"
alert(a1 != a3); //显示"true"
alert(a2 == a3); //显示"false"
alert(a2 != a3); //显示"true"
alert(a3 == a3); //显示"false"
alert(a3 != a3); //显示"true"
从上面的代码可以得出结论:(1)undefined与null是相等;(2)NaN与任何值都不相等,与自己也不相等。
JavaScript undefined 属性
定义和用法
undefined 属性用于存放 JavaScript 的 undefined 值。
语法
undefined
说明
无法使用 for/in 循环来枚举 undefined 属性,也不能用 delete 运算符来删除它。
undefined 不是常量,可以把它设置为其他值。
当尝试读取不存在的对象属性时也会返回 undefined。
提示和注释
提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
实例
在本例中,我们将检测两个变量中未定义的一个:
<script type="text/javascript">
var t1=""
var t2
if (t1===undefined) {document.write("t1 is undefined")}
if (t2===undefined) {document.write("t2 is undefined")}
</script>
输出:
t2 is undefined
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pytorch: tensor类型的构建与相互转换实例
pycharm配置当鼠标悬停时快速提示方法参数
![](https://img.aspxhome.com/file/2023/1/69601_0s.jpg)
.Net Core微服务网关Ocelot基础介绍及集成
![](https://img.aspxhome.com/file/2023/6/126206_0s.jpg)
python实现词法分析器
![](https://img.aspxhome.com/file/2023/6/93956_0s.png)
Docker-Compose创建mysql容器详解
![](https://img.aspxhome.com/file/2023/3/124463_0s.png)
Python Pygame实战之飞机大战的实现
![](https://img.aspxhome.com/file/2023/7/97107_0s.jpg)
XML:OpenSearch 浏览器指定搜索应用
![](https://img.aspxhome.com/file/UploadPic/20105/4/2008101720221064077801-78s.png)
基于Python绘制子图及子图刻度的变换等的问题
![](https://img.aspxhome.com/file/2023/3/90443_0s.png)
一个不错的javascript加密解密算法源码
php+mysql实现简单登录注册修改密码网页
![](https://img.aspxhome.com/file/2023/7/132787_0s.jpg)
Python 使用指定的网卡发送HTTP请求的实例
Python+Seaborn绘制分布图的示例详解
![](https://img.aspxhome.com/file/2023/2/98872_0s.png)
详解python调用cmd命令三种方法
python代码 FTP备份交换机配置脚本实例解析
![](https://img.aspxhome.com/file/2023/7/70807_0s.png)
OpenCV半小时掌握基本操作之边界填充
![](https://img.aspxhome.com/file/2023/8/131028_0s.gif)
vscode使用chatGPT 的方法
![](https://img.aspxhome.com/file/2023/8/104958_0s.png)
Django实现单用户登录的方法示例
![](https://img.aspxhome.com/file/2023/6/88896_0s.png)
splice slice区别
图文详解Python如何导入自己编写的py文件
![](https://img.aspxhome.com/file/2023/3/91943_0s.png)