第十天,js之undefined和null

undefined:表示没有为变量设置值,或者属性不存在。

null:表示变量是有值的,只是值为null。

但是如果不进行精确比较,很多时候undefined和null本身就相等。即undefined == null 返回true。要区分他们,应该用===


代码演示: 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="undefined_null.js"></script>
	</head>
	<body>
	</body>
</html>
var x,y = null;
if(x === undefined){
	alert("声明变量后默认值为undefined");
}
if(x === null){
	alert("声明变量后默认值为null");
}
if(x == y){
	alert(undefined == null);
}
if(String.abcde == undefined){
	alert("不存在的属性值默认为undefined");
}

实现:


undefined并不是java的保留字,在ECMAScript 3标准规范中,undefined就是一个全局变量,他的值就是他本身。

有些浏览器可能不支持undefined值,只要在脚本的第一行写上如下代码即可。

var undefined;

猜你喜欢

转载自blog.csdn.net/qq_38006520/article/details/81070506