简体   繁体   English

为什么`(“”||“word”)== true`返回false?

[英]Why `(“” || “word”) == true` returns false?

I'm currently learning JavaScript and I am really confused about this. 我目前正在学习JavaScript,我对此非常困惑。

Can anyone explain how this is possible : 任何人都可以解释这是如何可能的:

"" || "word" // -> "word"

("word") && true // -> true

("" || "word") == true // -> false

I have tried to search online but can't find the proper words to describe my problem. 我试图在网上搜索,但找不到合适的词来描述我的问题。

1. Question 1 1.问题1

 "" || "word" // -> "word" 

As described in the documentation on || ||文档中所述 :

Logical OR ( || ) 逻辑OR( ||
expr1 || expr2
Returns expr1 if it can be converted to true ; 如果可以转换为true ,则返回expr1 ; otherwise, returns expr2 . 否则,返回expr2

In this case, the first expression "" is falsy , so the second term is the outcome of the expression. 在这种情况下,第一个表达式"" 是假的 ,所以第二个术语是表达式的结果。 If the first expression would have been truthy, the second term would not have been evaluated, and the first one would be returned. 如果第一个表达式是真实的,则第二个术语将不会被评估,并且第一个表达式将被返回。

2. Question 2 2.问题2

 ("word") && true // -> true 

As described in the documentation on && : &&文档中所述:

Logical AND ( && ) 逻辑AND( &&
expr1 && expr2
Returns expr1 if it can be converted to false ; 如果可以转换为false ,则返回expr1 ; otherwise, returns expr2 . 否则,返回expr2

Here the first expression is truthy and so JavaScript returns the last one. 这里第一个表达式是真实的,因此JavaScript返回最后一个。 If the first one would have been falsy, the return value would have been the first term. 如果第一个是假的,那么返回值将是第一个术语。

3. Question 3 3.问题3

 ("" || "word") == true // -> false 

As per the first expression outcome, this is equivalent to: 根据第一个表达结果,这相当于:

 "word" == true // -> false 

With the == operator the coercion happens differently. 使用==运算符,强制发生的方式不同。 true is coerced into a string value , ie "1" , which evidently is not the same as "word" . true被强制转换为字符串值 ,即"1" ,显然与"word" If the first term would have been "1" or just 1, it would have yielded true : 如果第一个术语是"1"或只是1,它就会产生true

"1" == true // -> true
1 == true // -> true

Practical use 实际使用

If you have a condition like the last one, just write it without the comparison: 如果你有一个类似于最后一个的条件,只需编写它而不进行比较:

if ("" || "word") {
    console.log('it is truthy!');
}

Or if you really need true as the result, convert it explicitly to boolean with !! 或者如果您确实需要true作为结果,请将其显式转换为boolean with !! :

result = !!("" || "word");

There's more to truth in JavaScript than true or false . JavaScript中的真相不仅仅是truefalse

There are also truthy values and falsey values, and everything is either truthy or falsey. 还有一些真实的价值观和虚假的价值观,一切都是真实的或虚假的。

false , 0 , "" , null , undefined , and NaN are falsey. false0""nullundefinedNaN都是假的。
Everything else is truthy. 其他一切都是真实的。

The logical operators operate on truthy and falsey things; 逻辑运算符在真实和虚假的东西上运作;

a && b

is

  • a , if a is falsey a ,如果a是假的
  • otherwise, it is b 否则,是b

and

a || b

is

  • a , if a is truthy a ,如果a是truthy
  • otherwise, it is b 否则,是b

(If you're familiar with Lisp or Python, this is perfectly natural. If you're not, you're probably thinking "WAT?!".) (如果你熟悉Lisp或Python,这很自然。如果你不熟悉,你可能会想“WAT ?!”。)

Hence, 因此,

"" || "word"  -> "word"

because "" is falsey, 因为""是假的,

"word" && true  -> true

because "word" is truthy, and 因为"word"是真的,而且

("" || "word") == true  -> false

because "" || "word" 因为"" || "word" "" || "word" is "word" , and "word" is not the same object as true . "" || "word""word" ,而"word"true不是同一个对象。

I assume you understand the first two boolean, and have some confusion in the last one. 我假设您理解前两个布尔值,并且在最后一个布局中有一些混淆。

Your question is, if first 2 are true , why the last one is not true . 你的问题是,如果前两个是true ,为什么最后一个不是true

There is a concept of thruthy and falsey values in Javascript. 在Javascript中有一个thruthyfalsey值的概念。 A thruthy value may or may not be a Boolean , but still satisfies the if conditions. 一个thruthy值可能是也可能不是Boolean ,但仍满足if条件。

For example, in case of string, an empty string is a falsey value, and a string with even a single character is thruthy . 例如,在string的情况下,空字符串是falsey值,并且甚至单个字符的字符串都是thruthy

A truthy value is equivalent to true , but not equals to true . truthy等于 true ,但不等于 true Same for falsey . 同样是falsey

In the last case, ("" || "word") == true , the left part equals word , and the operator used, == checks if the left value equals right value. 在最后一种情况下, ("" || "word") == true ,左边的部分等于word ,运算符使用, ==检查左边的值是否等于正确的值。 So word is not equal to true , hence returns false . 所以word不等于true ,因此返回false

("" || "word") == true // lets simplify
"word" == true // false, "word" is not equal to true

But if you use word in an if condition, it will satisfy the condition since it's a thruthy value. 但是如果你在if条件中使用word ,那么它将满足条件,因为它是一个thruthy值。

if ("word") { /* condition will be satisfied */ }
if ("") { /* condition will NOT be satisfied */ }

To check whether a value is truthy or not, use !! 要检查值是否truthy ,请使用!! before the value. 在价值之前。

// String
!!"word" // true
!!""     // false
// Number
!!5      // true
!!0      // false
// Boolean
!!true   // true
!!false  // false

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

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