javascript 布尔_JavaScript布尔说明-如何在JavaScript中使用布尔

javascript 布尔

布尔型 (Boolean)

Booleans are a primitive datatype commonly used in computer programming languages. By definition, a boolean has two possible values: true or false.

布尔值是计算机编程语言中常用的原始数据类型。 根据定义,布尔值有两个可能的值: truefalse

In JavaScript, there is often implicit type coercion to boolean. If for example you have an if statement which checks a certain expression, that expression will be coerced to a boolean:

在JavaScript中,布尔值通常存在隐式类型强制。 例如,如果您有一个if语句检查某个表达式,则该表达式将被强制转换为布尔值:

const a = 'a string';
if (a) {
  console.log(a); // logs 'a string'
}

There are only a few values that will be coerced to false:

只有少数几个值会被强制设置为false:

  • false (not really coerced as it already is false)

    假(因为已经是假,所以没有被强制)
  • null

    空值
  • undefined

    未定义
  • NaN

    N
  • 0

    0
  • "" (empty string)

    “”(空字符串)

All other values will be coerced to true. When a value is coerced to a boolean, we also call that either ‘falsy’ or ‘truthy’.

所有其他值将被强制为true。 当值强制为布尔值时,我们也称其为“ falsy”或“ truthy”。

One way that type coercion is used is with the use of the or (||) and and (&&) operators:

使用类型强制的一种方式是使用or( || )和and( && )运算符:

const a = 'word';
const b = false;
const c = true;
const d = 0
const e = 1
const f = 2
const 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'

As you can see, the or operator checks the first operand. If this is true or truthy, it returns it immediately (which is why we get ‘word’ in the first case & true in the second case). If it is not true or truthy, it returns the second operand (which is why we get ‘word’ in the third case).

如您所见, or运算符检查第一个操作数。 如果这是对还是错,它会立即返回它(这就是为什么我们在第一种情况下得到“单词”而在第二种情况下得到true的原因)。 如果它不是对或不对,则返回第二个操作数(这就是为什么在第三种情况下我们得到“单词”的原因)。

With the and operator it works in a similar way, but for ‘and’ to be true, both operands need to be truthy. So it will always return the second operand if both are true/truthy, otherwise it will return false. That is why in the fourth case we get true and in the last case we get ‘word’.

使用and运算符,其工作方式类似,但是要使“ and”为真,两个操作数都必须为真。 因此,如果两个操作数均为true / true,它将始终返回第二个操作数,否则将返回false。 这就是为什么在第四种情况下我们为真,而在最后一种情况下我们为“单词”的原因。

布尔对象 (The Boolean Object)

There is also a native JavaScript object that wraps around a value. The value passed as the first parameter is converted to a boolean value, if necessary. If value is omitted, 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string “false”, create an object with an initial value of true.

还有一个原生JavaScript对象,它包装了一个值。 如果需要,作为第一个参数传递的值将转换为布尔值。 如果省略value,0,-0,null,false,NaN,undefined或空字符串(“”),则对象的初始值为false。 所有其他值,包括任何对象或字符串“ false”,都会创建一个初始值为true的对象。

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

不要将原始布尔值true和false与布尔对象的true和false值混淆。

更多细节 (More Details)

Any object whose value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. If true, this will execute the function. For example, the condition in the following if statement evaluates to true:

值不为undefined或null的任何对象(包括值为false的布尔对象)在传递给条件语句时都将计算为true。 如果为true,则将执行该功能。 例如,以下if语句中的条件评估为true:

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

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

此行为不适用于布尔基元。 例如,以下if语句中的条件评估为false:

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

Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:

不要使用布尔对象将非布尔值转换为布尔值。 而是使用Boolean作为执行此任务的函数:

const x = Boolean(expression);     // preferred
const x = new Boolean(expression); // don't use

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

如果指定任何对象(包括值为false的布尔对象)作为布尔对象的初始值,则新的布尔对象的值为true。

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

Do not use a Boolean object in place of a Boolean primitive.

不要使用布尔对象代替布尔基元。

翻译自: https://www.freecodecamp.org/news/booleans-in-javascript-explained-how-to-use-booleans-in-javascript/

javascript 布尔

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值