布尔 对象是用于布尔值的对象包裹器。

描述

The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0 , -0 , null , false , NaN , undefined , or the empty string ( "" ), the object has an initial value of false . All other values, including any object, an empty array ( [] ), or the string " false ", create an object with an initial value of true .

不要混淆 primitive 布尔 true and false 采用 true and false values of the 布尔 对象。

任何 object of which the value is not undefined or null , including a 布尔 object whose value is false , evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true :

var x = new Boolean(false);
if (x) {
  // this code is executed
}
					

This behavior does not apply to 布尔 primitives. For example, the condition in the following if statement evaluates to false :

var x = false;
if (x) {
  // this code is not executed
}
					

Do not use a 布尔 object to convert a non-boolean value to a boolean value. To perform this task, instead, use 布尔 as a function, or a double NOT operator :

var x = Boolean(expression);     // use this...
var x = !!(expression);          // ...or this
var x = new Boolean(expression); // don't use this!
					

If you specify any object, including a 布尔 object whose value is false , as the initial value of a 布尔 object, the new 布尔 object has a value of true .

var myFalse = new Boolean(false);   // initial value of false
var g = Boolean(myFalse);       // initial value of true
var myString = new String('Hello'); // string object
var s = Boolean(myString);      // initial value of true
					

Do not use a 布尔 object in place of a 布尔 primitive.

注意: When the non-standard property document.all is used as an argument for this constructor, the result is a 布尔 object with the value false . This property is legacy and non-standard and should not be used.

构造函数

Boolean()
创建新的 布尔 对象。

实例方法

Boolean.prototype.toString()
Returns a string of either true or false depending upon the value of the object. Overrides the Object.prototype.toString() 方法。
Boolean.prototype.valueOf()
Returns the primitive value of the 布尔 object. Overrides the Object.prototype.valueOf() 方法。

范例

采用 false 初始值创建布尔对象

var bNoParam = new Boolean();
var bZero = new Boolean(0);
var bNull = new Boolean(null);
var bEmptyString = new Boolean('');
var bfalse = new Boolean(false);
					

创建 布尔 对象采用初始值 true

var btrue = new Boolean(true);
var btrueString = new Boolean('true');
var bfalseString = new Boolean('false');
var bSuLin = new Boolean('Su Lin');
var bArrayProto = new Boolean([]);
var bObjProto = new Boolean({});
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Boolean' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动 Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
布尔 Chrome 1 Edge 12 Firefox 1 IE 3 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100
Boolean() 构造函数 Chrome 1 Edge 12 Firefox 1 IE 3 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100
toSource 非标 Chrome 不支持 No Edge 不支持 No Firefox 不支持 1 — 74 注意事项
1 — 74 注意事项
Starting in Firefox 74, toSource() is no longer available for use by web content. It is still allowed for internal and privileged code.
IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 4 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No nodejs 不支持 No
toString Chrome 1 Edge 12 Firefox 1 IE 3 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100
valueOf Chrome 1 Edge 12 Firefox 1 IE 4 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

图例

完整支持

完整支持

不支持

不支持

非标。预期跨浏览器支持较差。

见实现注意事项。

另请参阅

元数据

  • 最后修改: