View Full Version : difference between | and || ?

Nov 30th, 2002, 10:48 AM
hello I am confused about the difference between | and || I know one of them is bitwise and the other logic but I dont know which nor the difference. Its wierd because Ive seen myselft unitenionaly use both of them in different scripts, not seeing any differece in my results:confused: . I also know that theres && and & whats the difference there too? Please give me some info on this

Nov 30th, 2002, 01:57 PM
Hiya :)

|| is the logic OR, and | is the bitwise OR :)

the same applies to AND... & = bitwise, && = logic.

= is set equal to, == is check if equal to, and === is check if absolutely equal to.

...any others?

::] krycek [::

Nov 30th, 2002, 02:39 PM
If you use || or | as logical comparison operators in an if-statement, there is a difference concerning the evaluation of the operands:

var x = "foo";

function sampleFunc() {
x = "bar";
return true;

if ( x == "foo" || sampleFunc() ) {
document.write(x + "<br>");

if ( x == "foo" | sampleFunc() ) {
document.write(x + "<br>");

The above will write


which indicates that when you use || and the left comparison returns false, the expression right of the || is not carried out. Whereas with |, and the left expressions yields "false", the second expression (in our example 'sampleFunc()') is actually carried out.

Though I have to admit that you better should not write code that relies on this particular language feature, since it's very uncommon and hard to spot while debugging your code.

Nov 30th, 2002, 05:16 PM
wouldn't it be more correct to say that the logic
operators test if something is true or false
for example
if(A==A) <-tests if A is equal to A

Where as the bitwise operators makes an or on bit level
for example.
A=number1 | number2;

which would result in the following

or 00001001=number2

this is what a bitwise operator do in most other
programming languages at least

Dec 1st, 2002, 09:03 AM
ok thanks a bunch, and one more question, I dont get the difference between == and === please explain that more

Dec 1st, 2002, 05:33 PM
'1' == 1 returns true
'1' === 1 returns false

It is a strict equality operator.

If we were to create our own ===operator, it would look something like:

operator===(arg1, arg2) {
return (arg1.constructor == arg2.constructor && arg1 == arg2);

Dec 1st, 2002, 08:25 PM
ahhhh, I see, I get it. Thanks abunch for that!