[英]Javascript logical operators and results
I know that the result of logical operations in most of the languages is either true, false or 1,0. 我知道大多数语言中逻辑运算的结果是true,false或1,0。 In Javascript I tried the following:
在Javascript中我尝试了以下内容:
alert(6||5) // => returns 6
alert(5||6) // => returns 5
alert(0||5) // => returns 5
alert(5||0) // => returns 5
alert(5||1) // => returns 5
alert(1||5) // => returns 1
alert(5&&6) // => returns 6
alert(6&&5) // => returns 5
alert(0&&5) // => returns 0
alert(5&&0) // => returns 0
alert(-1&&5) // => returns 5
alert(5&&-1) // => returns -1
So what is the result of logical operators? 那么逻辑运算符的结果是什么? If one operand is 0 or 1 then it works as expected.
如果一个操作数为0或1,则它按预期工作。 If both are nonzero and other than 1 then
如果两者都非零而不是1
or
, the first operand is returned or
情况下,返回第一个操作数 and
, the second operand is returned and
情况下,返回第二个操作数 Is this the general rule? 这是一般规则吗?
Another thing I dont know is the operator |
我不知道的另一件事是操作员
|
. 。
I have tried the operator |
我试过运算符
|
and gotten different results: 得到了不同的结果:
alert(5|8) // => returns 13
alert(8|5) // => returns 13
alert(-5|8) // => returs -5
alert(8|-5) // => returns -5
alert(0|1) // => returns 1
alert(1|0) // => returns 1
alert(1|1) // => returns 1
What does this operator actually do? 这个运营商实际上做了什么?
Since javascript is not a typed languaged any object can be used on logical operators, if this object is null, a false boolean, an empty string, a 0 or an undefined variable then it acts like a false
if it's anything else then it is like a true
由于javascript不是类型化的语言,因此任何对象都可以在逻辑运算符上使用,如果此对象为null,则为false boolean,空字符串,0或未定义的变量,如果它是其他任何内容,则它就像
false
一样然后它就像一个true
At the end of the logical operation the last checked value returns. 在逻辑运算结束时,返回最后一个检查值。
So 所以
6||2 6 || 2
Check first value -> "6"
6 = true
Go to next value -> "2"
2 = true
End of operation, return last value.
操作结束,返回最后一个值。
2 which would work the same as true if passed to another logical operation.
2如果传递给另一个逻辑操作,它将与true相同。
Edit: that was a wrong statement. 编辑:那是一个错误的陈述。
6||2
returns 6
because 6
acting as true
is enough to know the condition OR
is true without the need to check the next value. 6||2
返回6
因为6
作为true
就足以知道条件OR
是真的而无需检查下一个值。
It is really the same way as in 它实际上和中国一样
true||true 真真正||
Check first value -> "true"
Check next value -> "true"
return last value -> "true"
And for 6 && 0 && 2 对于6 && 0 && 2
First value 6 = true
Next value 0 = false
Stop operation here and returns the last checked value: 0. 在此处停止操作并返回上次检查的值:0。
The | | operator is a whole different thing, it simply peforms a logical OR on the bits of the input values, as explaned on the other answer by akp.
运算符是一个完全不同的东西,它简单地对输入值的位进行逻辑或运算,正如akp在另一个答案中所解释的那样。
Actually what you have derived are the pure digital results...like... 实际上你得到的是纯粹的数字结果......就像......
3 in binary is 011......
4 in binary is 100.....
when u perform 3|4......
it is equivalent to 011|100......i.e the OR operator which is the one of the bases of all logical operations
011
100
will give 111 ie 7.............
so u will get 3|4 as 7......
hope u understand..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.