Home > Web Front-end > JS Tutorial > body text

Example codes for three scenarios of boolean type in JavaScript

高洛峰
Release: 2016-12-06 13:22:51
Original
866 people have browsed it

Examples are as follows:

<!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(&#39;truetrue.............&#39;);
        }else{
            alert(&#39;falsefalse...........&#39;);
        }
    </script>
     
    <script language="javascript">
        /*
        *   一个对象不存在,为null时的true,false
        */
        var obj = document.getElementById(&#39;id&#39;);
        if(obj){ //虽然这里的obj为null,但是可以解读为false
            alert(&#39;obj不为null表示true&#39;);
        } else{
            alert(&#39;obj为null时可识别为false&#39;);
        }
    </script>
     
    <script language="javascript">
        /*
        * 一个对象不存在为undefined时的true,false
        */
        var a;
        if(a){//虽然a的值为undefined,但是这里可以解读为false
            alert(&#39;a的值不为undefined解读为true&#39;);
        } else {
            alert(&#39;a的值为undefined解读为false&#39;);
        }
    </script>
</body>
</html>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!