JavaScript編程中布爾對象的基本使用_基礎知識

來源:互聯網
上載者:User

Boolean(布爾)對象用於將非布爾值轉換為布爾值(true 或者 false)。

檢查布爾值
檢查布爾對象是 true 還是 false。
原始碼樣本:

<!DOCTYPE html><html><body>​<script>var b1=new Boolean(0);var b2=new Boolean(1);var b3=new Boolean("");var b4=new Boolean(null);var b5=new Boolean(NaN);var b6=new Boolean("false");​document.write("0 is boolean "+ b1 +"<br>");document.write("1 is boolean "+ b2 +"<br>");document.write("An empty string is boolean "+ b3 + "<br>");document.write("null is boolean "+ b4+ "<br>");document.write("NaN is boolean "+ b5 +"<br>");document.write("The string 'false' is boolean "+ b6 +"<br>");</script>​</body></html> 

測試結果:

0 is boolean false1 is boolean trueAn empty string is boolean falsenull is boolean falseNaN is boolean falseThe string 'false' is boolean true

建立 Boolean 對象
Boolean 對象代表兩個值:"true" 或者 "false"
下面的代碼定義了一個名為 myBoolean 的布爾對象:

var myBoolean=new Boolean();

如果布爾對象無初始值或者其值為:

0-0null""falseundefinedNaN

那麼對象的值為 false。否則,其值為 true(即使當自變數為字串 "false" 時)!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.