freeCodeCamp/guide/chinese/javascript/booleans/index.md

102 lines
3.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Boolean
localeTitle: 布尔
---
## 布尔
布尔值是计算机编程语言中常用的原始数据类型。根据定义,布尔值有两个可能的值: `true`或`false` 。
在Javascript中通常存在对boolean的隐式类型强制。例如如果你有一个if语句来检查某个表达式那么该表达式将被强制转换为布尔值
```javascript
var a = 'a string';
if (a) {
console.log(a); // logs 'a string'
}
```
只有少数值会被强制为false
* 假(不是真的被胁迫,因为它已经是假的)
* 空值
* 未定义
* 为NaN
* 0
* ''(空字符串)
所有其他值将被强制为true。 当一个值被强制转换为布尔值时,我们也称之为'falsy'或'truthy'。
使用类型强制的一种方法是使用or `||` 和and `&&` )运算符:
```javascript
var a = 'word';
var b = false;
var c = true;
var d = 0
var e = 1
var f = 2
var g = null
console.log(a || b); // 'word'
console.log(c || a); // true
console.log(b || a); // 'word'
console.log(e || f); // 1
console.log(f || e); // 2
console.log(d || g); // null
console.log(g || d); // 0
console.log(a && c); // true
console.log(c && a); // 'word'
```
如您所见, _or_运算符检查第一个操作数。如果这是真的或真实的,它会立即返回(这就是为什么我们在第一种情况下得到'word'而在第二种情况下得到true。如果它不是真或真它会返回第二个操作数这就是为什么我们在第三种情况下得到'word')。
使用和运算符它以类似的方式工作,但为了'和'是真的,两个操作数都需要真实。因此,如果两者都是真/真它将始终返回第二个操作数否则它将返回false。这就是为什么在第四种情况下我们得到了真实在最后一种情况下我们得到了'字'。
## 布尔对象
还有一个包含值的本机JavaScript对象。如有必要作为第一个参数传递的值将转换为布尔值。如果省略value0-0nullfalseNaNundefined或空字符串“”则对象的初始值为false。所有其他值包括任何对象或字符串“false”都会创建一个初始值为true的对象。
不要将原始布尔值true和false与布尔对象的true和false值混淆。
## 更多细节
任何值未定义或为null的对象包括值为false的Boolean对象在传递给条件语句时计算结果为true。如果为true则执行该函数。例如以下if语句中的条件求值为true
```javascript
var x = new Boolean(false);
if (x) {
// this code is executed
}
```
此行为不适用于布尔基元。例如以下if语句中的条件求值为false
```javascript
var x = false;
if (x) {
// this code is not executed
}
```
不要使用Boolean对象将非布尔值转换为布尔值。相反使用布尔值作为函数来执行此任务
```javascript
var x = Boolean(expression); // preferred
var x = new Boolean(expression); // don't use
```
如果指定任何对象包括值为false的Boolean对象作为Boolean对象的初始值则新的Boolean对象的值为true。
```javascript
var myFalse = new Boolean(false); // initial value of false
var g = new Boolean(myFalse); // initial value of true
var myString = new String('Hello'); // string object
var s = new Boolean(myString); // initial value of true
```
不要使用布尔对象代替布尔基元。
### 资源
* [布尔对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
* [布尔对象](https://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html)