JavaScript編程中布爾對(duì)象的基本使用
更新時(shí)間:2015年10月25日 15:11:22 投稿:goldensun
這篇文章主要介紹了JavaScript編程中布爾對(duì)象的基本使用,是JavaScript入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
Boolean(布爾)對(duì)象用于將非布爾值轉(zhuǎn)換為布爾值(true 或者 false)。
檢查布爾值
檢查布爾對(duì)象是 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 對(duì)象
Boolean 對(duì)象代表兩個(gè)值:"true" 或者 "false"
下面的代碼定義了一個(gè)名為 myBoolean 的布爾對(duì)象:
var myBoolean=new Boolean();
如果布爾對(duì)象無初始值或者其值為:
0 -0 null "" false undefined NaN
那么對(duì)象的值為 false。否則,其值為 true(即使當(dāng)自變量為字符串 "false" 時(shí))!
相關(guān)文章
JavaScript While 循環(huán)基礎(chǔ)教程
只要指定條件為 true,循環(huán)就可以一直執(zhí)行代碼,2007-04-04

