为什么JavaScript中的or运算符的行为与C中的不同? [英] Why does the or operator in JavaScript behave differently than in C?

查看:106
本文介绍了为什么JavaScript中的or运算符的行为与C中的不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码.

console.log("All" && 1); // 1
console.log("All" || 1); // "All" ?

如您所见,第一个表达式"All" && 1正确计算为1.我希望第二个表达式"All" || 1也会计算为1.但是,它的值为"All".为什么这样?

As you can see, the first expression, "All" && 1, correctly evaluates to 1. I expected the second expression, "All" || 1, to evaluate to 1 too. However, it evaluates to "All". Why so?

在C中,第二个表达式正确地计算为1.

In C, the second expression correctly evaluates to 1.

#include <stdio.h>

int main() {
    printf("%d\n", "All" && 1); // 1
    printf("%d\n", "All" || 1); // 1
    return 0;
}

为什么JavaScript的行为有所不同?

Why does JavaScript behave differently?

推荐答案

C中的逻辑运算符始终求值为布尔值.在C中,int 1表示true,而int 0表示false.这就是为什么两个表达式"All" && 1"All" || 1都求值为1的原因.他们在逻辑上都是正确的.为了澄清起见,请考虑以下程序.

The logical operators in C always evaluate to boolean values. In C, the int 1 represents true and the int 0 represents false. That's the reason why both the expressions, "All" && 1 and "All" || 1, evaluate to 1. Both of them are logically true. For clarification, consider the following program.

#include <stdio.h>

int main() {
    printf("%d\n", 20 && 10); // 1
    printf("%d\n", 20 || 10); // 1
    return 0;
}

在上面的程序中,即使20 && 1020 || 10表达式中没有1,它们的结果仍为1.这是有道理的,因为这两个表达式在逻辑上都是正确的.因此,它们的计算结果为1,与JavaScript中的true等效.

In the above program, the expressions 20 && 10 and 20 || 10 still evaluate to 1 even though there is no 1 in those expressions. This makes sense because both those expressions are logically true. Hence, they evaluate to 1 which is equivalent to true in JavaScript.

如果JavaScript的行为方式与C相同,则表达式"All" && 10"All" || 10的值将为布尔值true.但是,这不是逻辑运算符在JavaScript中的行为方式.并不是说他们有越野车.

If JavaScript behaved the way C did then the expressions "All" && 10 and "All" || 10 would evaluate to the boolean value true. However, that's not the way the logical operators behave in JavaScript. That's not to say that they are buggy.

JavaScript中的值具有真实性和虚假性的概念.例如,值true"All"10[10, 20]{ foo: 10 }x => 2 * x都是真实的.另一方面,值false""0undefinednull都是错误的.

Values in JavaScript have a notion of truthiness and falsity. For example, the values true, "All", 10, [10, 20], { foo: 10 }, and x => 2 * x are all truthy. On the other hand, the values false, "", 0, undefined, and null are falsy.

JavaScript的逻辑运算符并不总是像C那样对布尔值求值.相反,它们求值为其操作数之一.如果&&运算符是伪造的,则将其赋值为左操作数.否则,它将计算为正确的操作数.类似地,如果||运算符为真,则求值为左操作数.否则,它将计算为正确的操作数.

The logical operators of JavaScript don't always evaluate to boolean values like C does. Instead, they evaluate to one of their operands. The && operator evaluates to its left operand if it's falsy. Otherwise, it evaluates to the right operand. Similarly, the || operator evaluates to its left operand if it's truthy. Otherwise, it evaluates to the right operand.

现在,值"All"是真实的.因此,"All" && 1求值为右操作数(即1),而"All" || 1求值为左操作数(即"All").请注意,1"All"都是真实值,这意味着它们等效于C中的1(代表真实性).

Now, the value "All" is truthy. Hence, "All" && 1 evaluates to the right operand (i.e. 1) whereas "All" || 1 evaluates to the left operand (i.e. "All"). Notice that both 1 and "All" are truthy values, which means that they are equivalent to 1 (which represents truthiness) in C.

因此,不. JavaScript不是越野车.

Hence, no. JavaScript is not buggy.

这篇关于为什么JavaScript中的or运算符的行为与C中的不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆