JS中true和false的判断

来源:互联网 发布:淘宝搜索链接代码 编辑:程序博客网 时间:2024/05/23 23:42

简介

学习和使用js的时候对于true和false的判断总是非常纠结,接下来做一个总结。JS中属于弱类型语言,这在一定程度上提供了方便,同时也为理解加大了阻碍,其中“==”和“===”就是其一,“==”只注重值的相等,“===”注重值和类型都相等,具体看下边的实例。

一:“==”与“===”

                            var a;                 1:           if (a == null) {                                document.write("null==" + a);                            }                 2:           if (a == undefined) {                                document.write("undefined==" + a);                            }                 3:           if (a === null) {                                document.write("null===" + a);                            }                 4:           if (a === undefined) {                                document.write("undefined===" + a);                            }

本实例直接定义了一个没有初始化的“a”,此时输出的值应该是undefined。js中null和undefined都表示没有的意思,只不过侧重点不同而已。

1:采用“==”比较两者的数值关系,由于null和undefined表示同一个含义“没有”,也就是说值都是“没有”,这里的“a==null”为true。

2:采用“==”比较两者的数值关系,而a的输出值本身就是undefined,所以这里的“a==undefined”肯定为true。

3:采用“===”比较两者的数值和类型的关系,null一般针对的时object类型,而这里的a虽然在数值上都可以匹配null和undefined,但是他的类型并不确定,而这个不确定对应的值就是“undefined”,object和undefined显然不是同类型,所以“===”在值上是相等的,但是类型上不相等,所以“a===null”为false。

4:采用“===”比较两者的数值和类型的关系,a的输出值本身就是“undefined”,并且类型未定,注意“类型未定”本身就是一种类型,就像平时说的颜色“无色”,无色本身也是一种颜色,JS这种未定类型的标志暂时可以用“undefined”来理解,所以“a===undefined”为true。


二:变量作为if的直接判断条件

 var a,b=-1,c= 1,d= 0,e=null,f=undefined,g='',h="",i = false,j=true,k=[],l={};                            if (a) {                              document.write("as is true<br>");                            }else{                              document.write("a is false<br>");                            }                            if (b) {                              document.write("b is true<br>");                            }else{                              document.write("b is false<br>");                            }                            if (c) {                              document.write("c is true<br>");                            }else{                              document.write("c is false<br>");                            }                            if (d) {                              document.write("d is true<br>");                            }else{                              document.write("d is false<br>");                            }                            if (e) {                              document.write("e is true<br>");                            }else{                              document.write("e is false<br>");                            }                            if (f) {                              document.write("f is true<br>");                            }else{                              document.write("f is false<br>");                            }                            if (g) {                              document.write("g is true<br>");                            }else{                              document.write("g is false<br>");                            }                            if (h) {                              document.write("h is true<br>");                            }else{                              document.write("h is false<br>");                            }                            if (i) {                              document.write("i is true<br>");                            }else{                              document.write("i is false<br>");                            }                            if (j) {                              document.write("j is true<br>");                            }else{                              document.write("j is false<br>");                            }                            if (k) {                              document.write("k is true<br>");                            }else{                              document.write("k is false<br>");                            }                            if (l) {                              document.write("l is true<br>");                            }else{                              document.write("l is falsse<br>");                            }                            if (NaN) {                              document.write("NaN is true<br>");                            }else{                              document.write("NaN is false<br>");                            }                             document.write("m在这里不会输出"+m);                            if (m) {                              document.write("m is true<br>");                            }else{                              document.write("m is false<br>");                            }                         

输出值

a is falseb is truec is trued is falsee is falsef is falseg is falseh is falsei is falsej is truek is truel is trueNaN is false

特别注意:明显m并没有输出,m在var中没有定义,此时如果把m当做一个变量去赋值,那么他会被作为全局变量对待,但是如果直接作为输出对象或者判断对象,此时js会报错,js在报错的情况下会停止执行。

结果分析:由输出结果可知,当一个变量的值是“”,‘’,0,undefined,null,false,或者是NaN时,作为if的判断条件时均作为false对待,其他为true,尤其当变量作为对象的时候只要这个对象变量的值不为null,那么就是true。

三:变量与Boolean同时作为if判断条件


原创粉丝点击