JavaScript中BOOLEAN类型之三种情景代码举例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
	<script language="javascript">
		/*
		* 一般性的true,false
		*/
		var flag = true;
		if(flag){
			alert('truetrue.............');
		}else{
			alert('falsefalse...........');
		}
	</script>
	
	<script language="javascript">
		/*
		*	一个对象不存在,为null时的true,false
		*/
		var obj = document.getElementById('id');
		if(obj){ //尽管这里的obj为null。可是能够解读为false
			alert('obj不为null表示true');
		} else{
			alert('obj为null时可识别为false');
		}
	</script>
	
	<script language="javascript">
		/*
		*  一个对象不存在为undefined时的true,false
		*/
		var a;
		if(a){//尽管a的值为undefined,可是这里能够解读为false
			alert('a的值不为undefined解读为true');
		} else {
			alert('a的值为undefined解读为false');
		}
	</script>
</body>
</html>

原文地址:https://www.cnblogs.com/llguanli/p/7196036.html