当前位置:Gxlcms > JavaScript > JavaScript中boolean类型之三种情景实例代码

JavaScript中boolean类型之三种情景实例代码

时间:2021-07-01 10:21:17 帮助过:5人阅读

实例如下:

<!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>

人气教程排行