注意:如果您是TypeScript新手,请先查看我们的TypeScript入门指南。
在TypeScript中,布尔值是基本数据类型,它们可以是true
或false
。例如:
let a = true;
let b = false;
注意:如果您将true或false用引号括起来,它们将被视为字符串。例如:
let a = 'true';
console.log(typeof a); // string
布尔值 运算符结果
TypeScript中的比较运算符和逻辑运算符始终返回布尔值。
1. TypeScript布尔值与比较运算符
let age: number = 18;
// Check if age is greater than or equal to 18
let isAdult: boolean = age >= 18;
console.log(isAdult); // true
这里,age >= 18
评估age是否大于或等于18,并将结果(true
或false
)赋给isAdult
。
2. TypeScript布尔值与逻辑运算符
let age: number = 18;
// Assign boolean value 'true' to hasConsent
let hasConsent: boolean = true;
let canDrive = age >= 16 && hasConsent;
console.log(canDrive); // true
在上面的程序中,age >= 16 && hasConsent
检查age是否大于或等于16以及hasConsent是否为true
。
这里,age >= 16
的值为true
。由于age >= 16
和hasConsent都为true
,因此canDrive的值也为true
。
更多关于布尔值
在TypeScript中,布尔值也用于在if...else
语句和for
循环中控制逻辑流程。例如:
let hasPermission: boolean = true;
// Boolean with if...else
if (hasPermission) {
console.log("Access granted");
}
else {
console.log("Access denied");
}
输出
Access granted
这里,如果布尔变量hasPermission为true
,则打印Access granted
。否则,打印Access denied
。
// Boolean with for loop
for (let i: number = 0; i < 3; i++) {
console.log("Loop iteration:", i);
}
输出
Loop iteration: 0 Loop iteration: 1 Loop iteration: 2
这里,布尔表达式i < 3
决定了for
循环中的迭代次数。
在TypeScript中,当某些数据类型在需要布尔值的上下文中(例如条件语句中)进行评估时,它们会被自动转换为布尔值。
以下是会被转换为特定布尔值的值得列表。
数据类型 | 布尔值 |
---|---|
undefined |
false |
null |
false |
NaN |
false |
'' |
false |
0 |
false |
20 |
true |
-20 |
true |
"hello" |
true |
TypeScript Boolean() 函数
Boolean()
函数用于将各种数据类型转换为布尔值。例如:
let a: string = "true";
console.log(Boolean(a)); // true
所有具有值的值都会返回true
。例如:
let result: boolean;
// Convert a positive number to boolean
result = Boolean(20);
console.log(result); // true
console.log(typeof result); // boolean
// Convert a negative number to boolean
result = Boolean(-20);
console.log(result); // true
// Convert a string to boolean
result = Boolean("hello");
console.log(result); // true
// Create an object
let obj = {a: 1};
// Convert the object to boolean
result = Boolean(obj)
console.log(result); // true
在TypeScript中,undefined
、null
、0、NaN
、''
会转换为false
。例如:
let result: boolean;
// Convert empty string to boolean
result = Boolean('');
console.log(result); // false
// Convert 0 to boolean
result = Boolean(0);
console.log(result); // false
// Convert undefined to boolean
result = Boolean(undefined);
console.log(result); // false
// Convert null to boolean
result = Boolean(null);
console.log(result); // false
// Convert NaN to boolean
result = Boolean(NaN);
console.log(result); // false
注意:如果您想了解更多关于布尔类型转换的信息,请访问TypeScript类型转换。
布尔对象
您可以使用new
关键字创建布尔对象。例如:
let a: boolean = true;
// Create a boolean object
let b = new Boolean(true);
console.log(a); // true
console.log(b); // [Boolean: true]
console.log(typeof a); // "boolean"
console.log(typeof b); // "object"
输出
true [Boolean: true] boolean object
注意:建议避免使用布尔对象,因为这会降低程序速度。
TypeScript布尔方法
TypeScript中一些内置的布尔方法有:
方法 | 描述 |
---|---|
toString() |
通过将布尔值转换为字符串来返回字符串值。 |
valueOf() |
返回布尔对象的原始值。 |
示例:将布尔值转换为字符串
let count: boolean = false;
// Convert the count variable to string
let result: string = count.toString();
console.log(result);
console.log(typeof result);
输出
false string
这里,我们使用toString()
方法将布尔变量count转换为了字符串。
示例:Boolean valueOf() 方法
// Create a boolean object
let boolObj = new Boolean(true);
// Get the primitive value of boolObj
let result: boolean = boolObj.valueOf();
console.log(boolObj); // [Boolean: true]
console.log(result); // true
console.log(typeof boolObj); // "object"
console.log(typeof result); // "boolean"
输出
[Boolean: true] true object boolean
这里,boolObj.valueOf()
返回布尔对象boolObj
的原始值。
注意:布尔对象的原始值就是存储在该对象中的原始布尔值,即true
或false
。
另请阅读