收藏本站 收藏本站
积木网首页 - 软件测试 - 常用手册 - 站长工具 - 技术社区
首页 > JavaScript > JS基础入门 > 正文

首页 - PHP - 数据库 - 操作系统 - 游戏开发 - JS - Android - MySql - Redis - MongoDB - Win8 - Shell编程 - DOS命令 - jQuery - CSS样式 - Python - Perl

Access - Oracle - DB2 - SQLServer - MsSql2008 - MsSql2005 - Sqlite - PostgreSQL - node.js - extjs - JavaScript vbs - Powershell - Ruby

JavaScript编程中布尔对象的基本使用

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 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

创建 Boolean 对象
Boolean 对象代表两个值:"true" 或者 "false"
下面的代码定义了一个名为 myBoolean 的布尔对象:

var myBoolean=new Boolean();

如果布尔对象无初始值或者其值为:

0
-0
null
""
false
undefined
NaN

那么对象的值为 false。否则,其值为 true(即使当自变量为字符串 "false" 时)!

实例解析JS布尔对象的toString()方法和valueOf()方法
toString()方法把布尔值转换为字符串,并返回结果。实例将布尔值转换为字符串:varbool=newBoolean(1);varmyvar=bool.toString();输出结果:true定义和用法toString()方

详解JavaScript编程中正则表达式的使用
RegExp:是正则表达式(regularexpression)的简写。什么是RegExp?正则表达式描述了字符的模式对象。当您检索某个文本时,可以使用一种模式来描述要检索

JavaScript中的数据类型转换方法小结
JavaScript变量可以转换为新变量或其他数据类型:通过使用JavaScript函数通过JavaScript自身自动转换将数字转换为字符串全局方法String()可以将数字转换为字

本周排行

更新排行

强悍的草根IT技术社区,这里应该有您想要的!
Copyright © 2010 Gimoo.Net. All Rights Rreserved  京ICP备05050695号