Javascript comparison and logical operators

These are a good reference, and are slightly different than other languages.

The conditional operator is one that I need to use a lot more.. it can reduce the lines of if then statements… variablename=(condition)?value1:value2
For example, lets do age verification for a bar in the U.S…
drinkable=(age<21)?”Too young”:”Old enough”;

Comparison operators:

Let’s say that x = 5 , then…

Operator Description Comparing Returns
== is equal to x==8 false
x==5 true
=== is exactly equal to (value and type) x===”5″ false
x===5 true
!= is not equal x!=8 true
!== is not equal (neither value nor type) x!==”5″ true
x!==5 false
> is greater than x>8 false
< is less than x<8 true
>= is greater than or equal to x>=8 false
<= is less than or equal to x<=8 true

Logical Operators

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true

One thought on “Javascript comparison and logical operators”

Leave a Reply