Logical Not Operator

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
s2009
Forum Newbie
Posts: 20
Joined: Thu Apr 30, 2009 1:20 am

Logical Not Operator

Post 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
Last edited by Benjamin on Sat May 02, 2009 12:30 pm, edited 2 times in total.
Reason: Added code tags.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Logical Not Operator

Post 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)
Last edited by kaszu on Sat May 02, 2009 2:03 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Logical Not Operator

Post by John Cartwright »

or "OR" :) (although they hold different precendence)
theo13
Forum Newbie
Posts: 2
Joined: Tue Apr 28, 2009 8:44 am

Re: Logical Not Operator

Post by theo13 »

Like the guys mentioned before attention: "|" is different than "||".
These kind of mistakes happen often. :)
s2009
Forum Newbie
Posts: 20
Joined: Thu Apr 30, 2009 1:20 am

Re: Logical Not Operator

Post 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!!
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Logical Not Operator

Post 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
Post Reply