简体   繁体   English

为什么 || JavaScript 中的 (or) 和 && (and) 运算符的行为与 C 中的不同(返回非布尔值)?

[英]Why does the || (or) and && (and) operator in JavaScript behave differently than in C (returning non boolean value)?

Consider the following code.考虑以下代码。

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

As you can see, the first expression, "All" && 1 , evaluates to 1 .如您所见,第一个表达式"All" && 1计算结果为1 Which is surely not a boolean value (not a true ).这肯定不是布尔值(不是true )。 I expected here more specifically true.我期望在这里更具体地真实。 Because I didn't force result as String .因为我没有将结果强制为String

The second expression, "All" || 1第二个表达式, "All" || 1 "All" || 1 , to evaluate to All . "All" || 1 、对All进行评价。 Which is also not a boolean value.这也不是布尔值。

In C language, the both expression evaluates to 1 .在 C 语言中, both 表达式的计算结果为1 As I force the result to convert in string.当我强制将结果转换为字符串时。

#include <stdio.h>

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

Why does JavaScript behave differently?为什么 JavaScript 的行为不同? More specifically Why JS returning a non boolean value?更具体地说,为什么 JS 返回一个非布尔值?

The logical operators in C always evaluate to boolean values. C 中的逻辑运算符始终计算为布尔值。 In C, the int 1 represents true and the int 0 represents false .在 C 中, int 1表示true , int 0表示false That's the reason why both the expressions, "All" && 1 and "All" || 1这就是为什么表达式"All" && 1"All" || 1 "All" || 1 , evaluate to 1 . "All" || 1 、评价为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;
}

In the above program, the expressions 20 && 10 and 20 || 10在上面的程序中,表达式20 && 1020 || 10 20 || 10 still evaluate to 1 even though there is no 1 in those expressions. 20 || 10仍计算为1 ,即使没有1中的表现。 This makes sense because both those expressions are logically true.这是有道理的,因为这两个表达式在逻辑上都是正确的。 Hence, they evaluate to 1 which is equivalent to true in JavaScript.因此,它们的计算结果为1 ,这相当于 JavaScript 中的true

If JavaScript behaved the way C did then the expressions "All" && 10 and "All" || 10如果 JavaScript 的行为方式与 C 相同,那么表达式"All" && 10"All" || 10 "All" || 10 would evaluate to the boolean value true . "All" || 10将评估为布尔值true However, that's not the way the logical operators behave in JavaScript.然而,这不是逻辑运算符在 JavaScript 中的行为方式。 That's not to say that they are buggy.这并不是说它们有问题。

Values in JavaScript have a notion of truthiness and falsity. JavaScript 中的值具有真假的概念。 For example, the values true , "All" , 10 , [10, 20] , { foo: 10 } , and x => 2 * x are all truthy.例如,值true"All"10[10, 20]{ foo: 10 }x => 2 * x都是真值。 On the other hand, the values false , "" , 0 , undefined , and null are falsy.另一方面,值false""0undefinednull是假的。

The logical operators of JavaScript don't always evaluate to boolean values like C does. JavaScript 的逻辑运算符并不总是像 C 那样计算布尔值。 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.否则,它计算为正确的操作数。

Now, the value "All" is truthy.现在,值"All"是真的。 Hence, "All" && 1 evaluates to the right operand (ie 1 ) whereas "All" || 1因此, "All" && 1计算为正确的操作数(即1 ),而"All" || 1 "All" || 1 evaluates to the left operand (ie "All" ). "All" || 1计算左操作数(即"All" )。 Notice that both 1 and "All" are truthy values, which means that they are equivalent to 1 (which represents truthiness) in C.请注意, 1"All"都是真值,这意味着它们等价于 C 中的1 (表示真值)。

Hence, no.因此,没有。 JavaScript is not buggy. JavaScript 没有问题。

I quote here from official doc :- Logical AND in JS我在此引用官方文档:- JS 中的逻辑与

The logical AND (&&) operator (logical conjunction) for a set of operands is true if and only if all of its operands are true.当且仅当其所有操作数为真时,一组操作数的逻辑 AND (&&) 运算符(逻辑与)为真。 It is typically used with Boolean (logical) values.它通常与布尔(逻辑)值一起使用。 When it is, it returns a Boolean value.当它是时,它返回一个布尔值。 However, the && operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.然而,&& 运算符实际上返回指定操作数之一的值,因此如果此运算符与非布尔值一起使用,它将返回一个非布尔值。

Let's take some examples,举几个例子吧

let a = [1,2,3];
console.log( 0 && a.b ); // return 0
console.log( 1 && a.b ); // return a type error.

In first console.log when JavaScript see 0 at first, JS interpreter stop and return first value.在第一个 console.log 中,当 JavaScript 首先看到0时,JS 解释器停止并返回第一个值。

This is happening because when JS interpreter translate first value to boolean, it evaluated to false .发生这种情况是因为当 JS 解释器将第一个值转换为布尔值时,它的计算结果为false We know the fact that " on any && operator if a single value is false , it will return false .我们知道“在任何&&运算符上,如果单个值为false ,它将返回false

So JS interpreter here try to save some computation power by returning before, without full statement evaluation.所以这里的 JS 解释器尝试通过返回 before 来节省一些计算能力,而不是完整的语句评估。 Which is great.这很棒。

The same is truth for LOGICAL OR (||) operator. LOGICAL OR (||)运算符也是如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么jQuery的行为与javascript不同? - Why does jQuery behave differently than javascript? 为什么&#39;&lt;&lt;&#39;运算符在Javascript和PHP中的行为有时会有所不同? - Why does the '<<' operator sometimes behave differently in Javascript and PHP? 为什么JavaScript“delete”运算符在不同的浏览器中表现不同? - Why does the JavaScript “delete” operator behave differently in different browsers? 为什么 oninput 事件在 Angular 中的行为与在 JavaScript 中的行为不同? - Why does the oninput event behave differently in Angular than it does in JavaScript? 为什么/ * * /注释的行为不同? Javascript错误? - Why does /* */ comment behave differently ? Javascript bug? 为什么这个函数表达式的行为与函数声明不同? - Why does this function expression behave differently than a function declaration? 为什么此全局Javascript变量在函数内部和外部的行为有所不同? - Why does this global Javascript variable behave differently inside and outside of a function? 为什么这个javascript对象在有和没有模块模式的情况下表现不同? - Why does this javascript object behave differently with and without a module pattern? 为什么&#39;+&#39;运算符在JavaScript中的行为不同于&#39;*&#39;,&#39;/&#39;和&#39;-&#39;运算符 - Why does the '+' operator behave different from '*' ,'/' and '-' operator in javascript 为什么“花括号”在JavaScript中的行为有所不同? - Why Curly Brackets behave differently in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM