User Rating: 2 / 5

Star ActiveStar ActiveStar InactiveStar InactiveStar Inactive

Comparison and Logical operators are used to test for true or false


Comparison operators are used in logical statements to determine equality or difference between variables or values.

Given that x=5, the table below explains the comparison operators:

Operators                        Description                  Comparing Returns
== equal to x==8 false
     x==5  true
  === exactly equal to x==="5" false
   (equal to value and equal type) x===5 true
 !=  not equal  x!8 true
 !== not equal (different values   x!=="5" true
   or different type)  x!==5  false
 > greater than  x>8 false 
 < less than   x<8 true
 >= greater than or equal to x>=8  false 
 <= less than or equal to   x<=8 true


Logical Operators are used to mark the relationship. Different variables or values

Given that x=6 and y=3, the table below explains the logical operators:

Operators         Description            Example

  &&                     and                 (x < 10 && y > 1) is true

  ||                       or                   (x==5 || y==5) is false

   !                       not                 !(x==y) is true


Conditional Operators

JavaScript also contains a conditional operato that assigns a value to a variable based on some condition.



Example: If the variable age is a value below 18, the value f the variable voteable will be "Too young, otherwise the value of voteable will be "Old enough": 

<p>Click the button to check the age.</p>
Age:<input id="age" value="18" />
<p>Old enough to vote?</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
function myFunction()
var age,voteable;
voteable=(age<18)?"Too young":"Old enough";
This is the result:





Chapter 12 - JavaScript Conditions