逐句学英文-布尔值

Boolean Valuesˈbu:li:ən布尔值

The boolean datatype has two values, these are represented by the literals true and false.
布尔数据类型有2个值,分别由直接量true和false表示。

Boolean values are generally the result of comparisons you make in your JavaScript programs. For example:
布尔值通常在JavaScript程序中比较所得的结果。例如:

a == 4

This code tests to see whether the value of the variable a is equal to the number 4.
这行代码检测了变量a的值是否和数值4相等。

If it is, the result of this comparison is the boolean value true.
如果相等,比较的结果就是布尔值true

If a is not equal to 4, the result of the comparison is false.
如果a不等于4,比较的结果就是false

Boolean values are typically used in JavaScript control structures.
布尔值通常用于JavaScript的控制结构。

For example, the if/else statement in JavaScript performs one action if a boolean value is true and another action if the value is false.
例如,JavaScript的 if/else 语句,就是在布尔值为true时,执行一个动作,而在布尔值为false时,执行另一个动作。

if (a == 4)
 
b = b + 1;
else
 
a = a + 1;

This code checks whether a equals 4. If so, it adds 1 to b; otherwise, it adds 1 to a.
这段代码检测了a是否等于4,如果是,就给b增加1,否则给a加1

Boolean Type Conversions 布尔类型转换

If a boolean value is used in a numeric context, true converts to the number 1 and false converts to the number 0.
如果一个布尔值用在数值环境中,true就转换为数字1,而false就转换为数字0

If a boolean value is used in a string context, true converts to the string “true” and false converts to the string “false”.
如果一个布尔值用在一个字符串环境中,true就会转换为字符串“true”,而false就转换为字符串“false”

If a number is used where a boolean value is expected, the number is converted to true unless the number is 0 or NaN, which are converted to false.
如果一个数字用在一个本该是布尔值的地方,那么,如果这个数字是0或NaN,它就会转换为false,否则就转换为true

If a string is used where a boolean value is expected, it is converted to true except for the empty string, which is converted to false.
如果字符串用在一个本该是布尔值的地方,那么空字符串会被转换为false,否则就会转换为true

null and the undefined value convert to false , and any non-null object, array, or function converts to true.
空值和未定义的值也会转换为false,而任何的非空对象、数组或函数都转换为true

If you prefer to make your type conversions explicit, you can use the Boolean() function:
如果你喜欢让类型转换为显式的,可以使用 Boolean() 函数:
var x_as_boolean = Boolean(x);

Another technique is to use the Boolean NOT operator twice:
另一种方法是使用布尔非运算符两次:
var x_as_boolean = !!x;

Referenceˈrefrəns参考

《JavaScript权威指南(影印版)(第5版)》 《JavaScript权威指南(第5版)》

崔凯博客
Powered by WordPress | Theme by ck web design | TOP 顶部
Copyright © 崔凯 All rights reserved. 蜀ICP备12025891号.