JavaScript編程中布爾對象的基本使用
Boolean(布爾)對象用于將非布爾值轉(zhuǎn)換為布爾值(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>
測試結(jié)果:
0 is boolean false 1 is boolean true An empty string is boolean false null is boolean false NaN is boolean false The string 'false' is boolean true
創(chuàng)建 Boolean 對象
Boolean 對象代表兩個值:"true" 或者 "false"
下面的代碼定義了一個名為 myBoolean 的布爾對象:
var myBoolean=new Boolean();
如果布爾對象無初始值或者其值為:
0 -0 null "" false undefined NaN
那么對象的值為 false。否則,其值為 true(即使當自變量為字符串 "false" 時)!
相關(guān)文章
轉(zhuǎn)義字符(\)對JavaScript中JSON.parse的影響概述
JSON是一個提供了stringify和parse方法的內(nèi)置對象,前者用于將js對象轉(zhuǎn)化為符合json標準的字符串,后者將符合json標準的字符串轉(zhuǎn)化為js對象,本文為大家介紹下轉(zhuǎn)義字符對JSON.parse方法的影響2013-07-07javascript小數(shù)四舍五入多種方法實現(xiàn)
javascript小數(shù)四舍五入在工作中經(jīng)常會使用到,今天本文整理了一些常用的小竅門,需要了解的朋友可以參考下2012-12-12JavaScript sup方法入門實例(把字符串顯示為上標)
這篇文章主要介紹了JavaScript sup方法入門實例,sup方法用于把字符串顯示為上標,需要的朋友可以參考下2014-10-10JavaScript中的Math.SQRT1_2屬性使用簡介
這篇文章主要介紹了JavaScript中的Math.SQRT1_2屬性的使用,是JS入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06