简体   繁体   English

JS 0 === (0 || 6); 返回 false - 为什么?

[英]JS 0 === (0 || 6); returns false - why?

0 === (0 || 6) // returns false
false === (false || 6) // returns false
(false === false || false === 6) // returns true
(0 === 0 || 0 === 6) // returns true

 console.log( 0 === (0 || 6) ); console.log( false === (false || 6) ); console.log( (false === false || false === 6) ); console.log( (0 === 0 || 0 === 6) );

what gives?是什么赋予了? I ran across this scenario earlier and don't get it.我之前遇到过这种情况,但不明白。

0 === (0 || 6) // returns false

It's all about order of operations.这都是关于操作顺序的。 That returns false because the parenthesis are evaluated first.这将返回 false,因为首先评估括号。 So (0 || 6) returns 6 and 0 === 6 is false所以(0 || 6)返回 6 并且0 === 6false

false === (false || 6) // returns false

(false === false || false === 6) // returns true

(0 === 0 || 0 === 6) // returns true

The rest are easy to follow after that explanation.在这个解释之后,其余的很容易理解。

0 === (0 || 6) is equivalent to 0 === 6 which is of course false. 0 === (0 || 6)等价于0 === 6这当然是错误的。

false === (false || 6) is equivalent to false === 6 which is also obviously false. false === (false || 6)等价于false === 6 ,这显然也是错误的。

(false === false || false === 6) is equivalent to true || false (false === false || false === 6)等价于true || false true || false which is of course true. true || false这当然是真的。

(0 === 0 || 0 === 6) is also equivalent to true || false (0 === 0 || 0 === 6)也等价于true || false true || false . true || false

Look up "operator precedence".查找“运算符优先级”。 That'll help you wrap your head around javascript in the future.这将帮助您在未来了解 javascript。

First, you should know the operators priority.首先,您应该知道运营商的优先级。

Second, you should know how the operators work and what they return.其次,您应该知道操作符是如何工作的以及它们返回什么。

Here is the documentation you can take it as reference . 这是您可以作为参考的文档

 console.log( 0 === (0 || 6) ); // (0 || 6) returns 6 because 0 is falsy, // so it becomes 0 === 6. It is obviously return false. console.log( false === (false || 6) ); (false || 6) is equals (0 || 6), so we get (false === 6) = false. console.log( (false === false || false === 6) ); // this is three part. === is prior to ||, // so we should do (false === false) and (false === 6). // (false === false) is true and (false === 6) is false, // so we get (true || false) = true console.log( (0 === 0 || 0 === 6) ); // (0 === 0) is true and (0 === 6) is false, // so true || false is true.

By the way, most of the JavaScript operator do type conversion whether the two sides is not the type the operator want.顺便说一下,大多数 JavaScript 运算符都会进行类型转换,无论两侧是否不是运算符想要的类型。 Here's something you can take a look:以下是您可以查看的内容:

https://javascript.info/type-conversions https://javascript.info/type-conversions

From your question, I am guessing you are not familiar with how AND & OR are used or works.从您的问题来看,我猜您不熟悉AND & OR的使用或工作方式。

So,所以,

| stands for bitwise OR代表按位OR

and, & stands for bitwise AND and, &代表按位AND

It is a common method of writing AND as && and OR as ||AND写成&&OR写成||是一种常见的方法. .

For AND , following are the scenarios:对于AND ,以下是场景:

 1 && 1 => 1
 1 && 0 => 0
 0 && 1 => 0
 0 && 0 => 0

For OR , following are the scenarios:对于OR ,以下是场景:

 1 || 1 => 1
 1 || 0 => 1
 0 || 1 => 1
 0 || 0 => 0

In the above, 1 can be any non-zero integer or value (but we denote it by one, meaning high ).在上面, 1可以是任何非零整数或值(但我们用 1 表示它,意思是)。

So, if you write,所以,如果你写,

0 === ( 0 || 6 ) , 0 === ( 0 || 6 ) ,

then, ( 0 || 6 ) is already true , then, if you compare true with 0 , then it is always going to be false.那么, ( 0 || 6 )已经是true ,那么,如果你将true0进行比较,那么它总是会是 false 。

Whereas, if you you change the above condition as:然而,如果您将上述条件更改为:

0 == ( 0 && 6 ) , ( EDITTED ) 0 == ( 0 && 6 ) , ( 已编辑)

( 0 && 6 ) becomes false, and then 0 == false becomes true . ( 0 && 6 )变为假,然后0 == false变为true

=== and == are different because the second one just checks if they are equal or not, but it does not check if they are exactly same. =====是不同的,因为第二个只是检查它们是否相等,而不检查它们是否完全相同。 === checks if the values are identical, meaning exactly same. ===检查值是否相同,即完全相同。

I hope my answer was helpful.我希望我的回答有帮助。

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

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