javaScript中奇葩的假值

來源:互聯網
上載者:User

標籤:==   nand   class   函數   布爾   正則   對象   沒有   false   

JavaScript中有 6 個值為“假”,這六個值是:

  1. false
  2. null
  3. undefined
  4. 0
  5. ‘‘(Null 字元串)
  6. NaN

這裡面 false 本身是布爾類型,其它 5 個則不是。

除了這 6 個外,其它均為“真” ,包括對象、數組、正則、函數等。注意 ‘0‘、‘null‘、‘false‘、{}、[]也都是真值  。

 

雖然這六個值都為“假”,它們之間並非都相等:

 1     console.log( false == null )      // false 2     console.log( false == undefined ) // false 3     console.log( false == 0 )         // true 4     console.log( false == ‘‘ )        // true 5     console.log( false == NaN )       // false 6  7     console.log( null == undefined ) // true 8     console.log( null == 0 )         // false 9     console.log( null == ‘‘ )        // false10     console.log( null == NaN )       // false11 12     console.log( undefined == 0)   // false13     console.log( undefined == ‘‘)  // false14     console.log( undefined == NaN) // false15 16     console.log( 0 == ‘‘ )  // true17     console.log( 0 == NaN ) // false

對於“==”,以上得出下列結論:

  • false 除了和自身比較為 true 外,和 0,"" 比較也為 true
  • null 只和 undefined 比較時為 true, 反過來 undefined 也僅和 null 比較為 true,沒有第二個
  • 0 除了和 false 比較為 true,還有Null 字元串 ‘‘" 和空數組 []
  • Null 字元串 ‘‘ 除了和 false 比較為 true,還有一個數字 0

 參考串連:http://www.cnblogs.com/snandy/p/3589517.html

javaScript中奇葩的假值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.