简体   繁体   English

为什么这段代码在JavaScript中产生3?

[英]Why does this code produce 3 in JavaScript?

Why does the following code produce a == 3 ? 为什么以下代码产生a == 3

var x = "abc";
var y = 3;
var z = "xyz";
var a = x && y || z;

http://jsfiddle.net/thinkingmedia/qBZAL/ http://jsfiddle.net/thinkingmedia/qBZAL/

I would have expected this to result in a == true . 我原以为这会导致a == true

Why is the logical operator evaluating "abc" as true but doesn't evaluate 3 as true . 为什么逻辑运算符将"abc"评估为true但不将3评估为true Instead it produces 3 as the result. 相反,它产生3作为结果。

Furthermore, if you change y = 0 then a == "xyz" which means that && treated 0 as false . 此外,如果更改y = 0a == "xyz" ,这意味着&&0视为false What happen to treating a number as a number? 将数字视为数字会怎样?

What's going on here with the logical operators? 逻辑运算符在这里发生了什么?

This is to be expected. 这是可以预料的。

In JavaScript (and many other languages), not only Booleans themselves are true or false, but other objects can be truthy or falsey as well (See the docs on mdn ): 在JavaScript(以及许多其他语言)中,不仅布尔本身本身也是假的,但其他对象也可能是真实的或虚假的(参见mdn上的文档 ):

The value […] is converted to a boolean value, if necessary. 如有必要,值[...]将转换为布尔值。 If value is […] is 0 , -0 , null , false , NaN , undefined , or the empty string ( "" ), [it is] false. 如果值为[...]为0-0nullfalseNaNundefined或空字符串( "" ),则[false]为false。 All other values, including any object or the string "false" , create […] true. 所有其他值,包括任何对象或字符串"false" ,都会创建[...] true。

The logical operators || 逻辑运算符|| and && don't return true or false , rather they return the last argument to influence whether they are truthy or falsey ( reference ): &&不返回truefalse ,而是返回最后一个参数来影响它们是真或假( 参考 ):

  • expr1 && expr2 – Returns expr1 if it can be converted to false; expr1 && expr2 - 如果可以转换为false,则返回expr1 ; otherwise, returns expr2 . 否则,返回expr2 Thus, when used with Boolean values, && returns true if both operands are true; 因此,当与布尔值一起使用时,如果两个操作数都为真,则&&返回true; otherwise, returns false. 否则,返回false。
  • expr1 || expr2 expr1 || expr2 – Returns expr1 if it can be converted to true; expr1 || expr2 - 如果可以转换为true,则返回expr1 ; otherwise, returns expr2 . 否则,返回expr2 Thus, when used with Boolean values, || 因此,当与布尔值一起使用时, || returns true if either operand is true; 如果任一操作数为true,则返回true; if both are false, returns false. 如果两者都为假,则返回false。
  1. The first step is to evaluate "abc" && 3 . 第一步是评估"abc" && 3

    • false && 3 would return false , false && 3会返回false
    • true && 3 would return 3 . true && 3将返回3

    "abc" is not false, so JavaScript takes the second part, ie 3. “abc”不是假的,因此JavaScript采用第二部分,即3。

The second step is to evaluate 3 || "xyz" 第二步是评估3 || "xyz" 3 || "xyz" . 3 || "xyz" Here, JavaScript takes the first value which is not null, ie 3. This is similar to this.firstObject ?? this.defaultValue 这里,JavaScript获取第一个非空值,即3.这类似于this.firstObject ?? this.defaultValue this.firstObject ?? this.defaultValue in C#: the second part is taken only when the first part is null. C#中的this.firstObject ?? this.defaultValue :仅当第一部分为空时才采用第二部分。

the side effect is that you can do things like this: 副作用是你可以做这样的事情:

x = x || {};

to set a variable to a default if it is not set. 如果未设置,则将变量设置为默认值。

Or 要么

TrackingManager && TrackingManager.track("user was here")

to avoid bulkier if statements. 避免笨重的if语句。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM