Page 1 of 1

Logical Not Operator

Posted: Sat May 02, 2009 12:28 pm
by s2009
Hi all

I am a beginner learning Javascript.

When I run the following program :

Code: Select all

 
<html>
<head>
<title> Logical Operator </title>
</head>
<body>
<script type = "text/javascript">
var a = 6;
var b = 3;
var c = (!(a==b));
document.write("Result of Negation Operator");
document.write(c);
document.write("<br />");
var d = (a <10 && b > 1);
document.write("Result of And Operator");
document.write(d);
document.write("<br />");
var e =(a==5 | b==5);
document.write("Result of Or Operator");
document.write(e);
</script>
</body>
</html>
 
I am getting the following output :
Result of Negation Operator true
Result of And Operator true
Result of Or Operator 0

Can any body let me know why for OR OPERATOR alone it is displaying 0?

What should I do to get True or False value using OR OPERATOR?

Please help me out.

Thanks in advance

Re: Logical Not Operator

Posted: Sat May 02, 2009 1:53 pm
by kaszu
Or operator is ||

Code: Select all

var e =(a==5 || b==5);
| is bitwise OR operator. See http://lv.php.net/manual/en/language.op ... itwise.php (not javascript, but the you will get the idea)

Re: Logical Not Operator

Posted: Sat May 02, 2009 1:54 pm
by John Cartwright
or "OR" :) (although they hold different precendence)

Re: Logical Not Operator

Posted: Sat May 02, 2009 4:17 pm
by theo13
Like the guys mentioned before attention: "|" is different than "||".
These kind of mistakes happen often. :)

Re: Logical Not Operator

Posted: Sun May 03, 2009 12:04 pm
by s2009
Ok.

Thanks for your reply.

Can any body tell me with example what is the difference between the Logial OR and the BitWise OR?

Please reply.

Thanks in advance!!

Re: Logical Not Operator

Posted: Sun May 03, 2009 12:47 pm
by kaszu
Logical OR:

Code: Select all

A     | B     | A || B
true  | true  | true
false | false | false
true  | false | true
false | true  | true
In javascript:
* true is any number except 0, boolean true, non-empty string, etc.
* false is 0, empty string, null, undefined, etc.

Bitwise OR works with bits of a number.
Lets say A is 00001001 (binary representation of 9) and B is 00000011 (binary representation of 3)
Bitwise OR works similar to logical OR, but only for each bit:

00001001 <- A == 9
00000011 <- B == 3
00001011 <- A | B == 11