--- title: Boolean slug: Web/JavaScript/Reference/Global_Objects/Boolean tags: - Boolean - Constructor - JavaScript translation_of: Web/JavaScript/Reference/Global_Objects/Boolean --- <p>{{JSRef}}</p> <p><strong><code>Boolean</code></strong>对象是一个布尔值的对象包装器。</p> <h2 id="描述">描述</h2> <p>如果需要,作为第一个参数传递的值将转换为布尔值。如果省略或值<code>0</code>,<code>-0</code>,{{jsxref("null")}},<code>false</code>,{{jsxref("NaN")}},{{jsxref("undefined")}},或空字符串(<code>""</code>),该对象具有的初始值<code>false</code>。所有其他值,包括任何对象,空数组(<code>[]</code>)或字符串<code>"false"</code>,都会创建一个初始值为<code>true</code>的对象。</p> <p>注意不要将基本类型中的布尔值 <code>true</code> 和 <code>false</code> 与值为 <code>true</code> 和 <code>false</code> 的 <code>Boolean</code> 对象弄混了。</p> <p>其值不是{{jsxref("undefined")}}或{{jsxref("null")}}的任何对象(包括其值为<code>false</code>的布尔对象)在传递给条件语句时都将计算为<code>true</code>。 例如,以下{{jsxref("Statements/if...else", "if")}}语句中的条件评估为<code>true</code>:</p> <pre class="brush: js notranslate">var x = new Boolean(false); if (x) { // 这里的代码会被执行 } </pre> <p>基本类型的布尔值不受此规则影响。例如下面的 {{jsxref("Statements/if...else", "if")}} 语句的条件为假:</p> <pre class="brush: js notranslate">var x = false; if (x) { // 这里的代码不会执行 } </pre> <p>不要用创建 <code>Boolean</code> 对象的方式将一个非布尔值转化成布尔值,直接将 <code>Boolean</code> 当做转换函数来使用即可,或者使用<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_Operators#%E9%80%BB%E8%BE%91%E9%9D%9E%EF%BC%88!%EF%BC%89">双重非(!!)运算符</a>:</p> <pre class="brush: js notranslate">var x = Boolean(expression); // 推荐 var x = !!(expression); // 推荐 var x = new Boolean(expression); // 不太好 </pre> <p>对于任何对象,即使是值为 <code>false</code> 的 <code>Boolean</code> 对象,当将其传给 <code>Boolean</code> 函数时,生成的 <code>Boolean</code> 对象的值都是 <code>true</code>。</p> <pre class="brush: js notranslate">var myFalse = new Boolean(false); // true var g = new Boolean(myFalse); // true var myString = new String("Hello"); var s = new Boolean(myString); // true </pre> <p>最后,不要在应该使用基本类型布尔值的地方使用 <code>Boolean</code> 对象。</p> <div class="blockIndicator note"> <p><strong>注意:</strong>当将非标准属性<a href="/zh-CN/docs/Web/API/Document#Properties">document.all</a>用作此构造函数的参数时,结果是值为<code>false</code>的布尔对象。 此属性是旧属性,是非标准属性,不应使用。</p> </div> <h2 id="构造器">构造器</h2> <dl> <dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean"><code>Boolean()</code></a></dt> <dd>创建一个新的<code>Boolean</code> 对象。</dd> </dl> <h2 id="实例方法">实例方法</h2> <dl> <dt>{{jsxref("Boolean.prototype.toString()")}}</dt> <dd>根据对象的值返回字符串<code>"true"</code>或<code>"false"</code>。 重写{{jsxref("Object.prototype.toString()")}}方法。</dd> <dt>{{jsxref("Boolean.prototype.valueOf()")}}</dt> <dd>返回{{jsxref("Boolean")}}对象的原始值。 重写{{jsxref("Object.prototype.valueOf()")}}方法。</dd> </dl> <h2 id="示例">示例</h2> <h3 id="创建值为_false_的_Boolean_对象">创建值为 <code>false</code> 的 <code>Boolean</code> 对象</h3> <pre class="brush: js notranslate">var bNoParam = new Boolean(); var bZero = new Boolean(0); var bNull = new Boolean(null); var bEmptyString = new Boolean(''); var bfalse = new Boolean(false); </pre> <h3 id="创建值为_true_的_Boolean_对象">创建值为 <code>true</code> 的 <code>Boolean</code> 对象</h3> <pre class="brush: js notranslate">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({}); </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <td>规范</td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-boolean-objects', 'Boolean')}}</td> </tr> </tbody> </table> <h2 id="浏览器兼容性"><br> 浏览器兼容性</h2> <p>{{Compat("javascript.builtins.Boolean")}}</p> <h2 id="相关链接">相关链接</h2> <ul> <li>{{jsxref("Boolean.prototype")}}</li> <li>{{Glossary("Boolean")}}</li> <li><a href="http://en.wikipedia.org/wiki/Boolean_data_type">Boolean data type (Wikipedia)</a></li> </ul>