zoukankan      html  css  js  c++  java
  • javascript之任意类型转换Boolean类型

    • javascript的Boolean类型

          javascript的Boolean类型,包含true和false两个值,由于javascript区分大小写,所以True和False不是Boolean类型的值。

    • javascript任意类型转换为Boolean类型

          javascript任意类型的值都有与Boolean类型值等价的值,要将值转换为对应Boolean值,可以调用Boolean()函数。

          转换规则如下:

    数据类型 转换为true值 转换为false值
    Boolean true false
    String 非空字符串 ""(空字符串)
    Number 非零数值 0和NaN
    Object 非空对象 null
    Undefined   undefined
     1   <script type="text/javascript">
     2   /**************Boolean类型转换*****************/
     3      var boolTrue=true;
     4      var showValue1=Boolean(boolTrue);
     5      alert(showValue1);//true
     6      
     7      var boolFalse=false;
     8      var showValue2=Boolean(boolFalse);
     9      alert(showValue2);//flase
    10      
    11   /**************String类型转换*****************/
    12     var strValue1="test";
    13     var showValue3=Boolean(strValue1);
    14     alert(showValue3);//true
    15     
    16     var strValue2="";
    17     var showValue4=Boolean(strValue2);
    18     alert(showValue4);//false
    19     
    20   /**************Number类型转换*****************/
    21     var numValue1=10;
    22     var showValue5=Boolean(numValue1);
    23     alert(showValue5);//true
    24     
    25     var numValue2=0;
    26     var showValue6=Boolean(numValue2);
    27     alert(showValue6);//false
    28     
    29  /**************Object对象转换*****************/
    30     var objValue1=new Object();
    31     var showValue7=Boolean(objValue1);
    32     alert(showValue7);//true
    33     
    34     var objValue2=null;
    35     var showValue8=Boolean(objValue2);
    36     alert(showValue8);//false
    37     
    38  /**************Undefined对象转换*****************/
    39     var udfValue;
    40     var showValue9=Boolean(udfValue);
    41     alert(showValue9);//false
    42   </script>
    • if条件为任意类型

          if条件为任意类型,将自动执行Boolean转换。转换规则如上。

    1     var message="test Boolean";
    2     if(message){
    3         alert("Value is true");
    4     }

         

  • 相关阅读:
    C语言-if语句
    C语言-表达式
    C语言-基础
    Java for LeetCode 187 Repeated DNA Sequences
    Java for LeetCode 179 Largest Number
    Java for LeetCode 174 Dungeon Game
    Java for LeetCode 173 Binary Search Tree Iterator
    Java for LeetCode 172 Factorial Trailing Zeroes
    Java for LeetCode 171 Excel Sheet Column Number
    Java for LeetCode 169 Majority Element
  • 原文地址:https://www.cnblogs.com/wanghonghu/p/3014007.html
Copyright © 2011-2022 走看看