Common JavaScript Manual/Logic and Comparison operators

From Wikibooks, open books for an open world
< Common JavaScript Manual
Jump to: navigation, search

Logic operators[edit]

There are three logical operators in JavaScript. It are "not","and","or". Let's see example.

Operator Action
|| or
&& and
 ! not
!true; // False
!false; // True
false || true; // True
false && true;// False

Comparison operators[edit]

In parts of the previous file you've already seen some of these operators, and certainly understand their meaning but in anyway. There are 8 comparison operators in JavaScript.

Operator Condition
== Equal
 != Not equal
=== Equal value and type
 !== Not equal value or type
> More
< Less
<= Less or equal
>= More or equal

And for example:

5 == 5; //True
5 === '5'; //False
5 != 3; //True
5 !== '3'; //True
5 > 3; //True
5 < 3; //False
5 <= 5; //True
5 >= 3; //True

XOR Hack[edit]

In JavaScript there is no operator XOR (also named Exclusive OR). But we can write function that use bitwise XOR and return booleadn XOR of two arguments.

xor = function(a,b){
  return Boolean(a ^ b)
}

And let's use it.

xor(true,false); //True
xor(true,true); //False
xor(false,false); //False

Break, continue, labels · Condition operator