I know that the result of logical operations in most of the languages is either true, false or 1,0. In Javascript I tried the following:
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. If both are nonzero and other than 1 then
- In case of logical
or, the first operand is returned - In case of logical
and, the second operand is returned
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?