Comparison and logical operators are used to test true or false.
Comparison operators
Comparison operators are used in logical statements to determine whether a variable or value is equal.
Given the x=5, the following table explains the comparison operator:
| operator |
Description |
Example |
| == |
Equals |
X==8 to False |
| === |
Congruent (value and type) |
X===5 is true;x=== "5" to False |
| != |
Not equal to |
X!=8 is True |
| > |
Greater than |
X>8 to False |
| < |
Less than |
X<8 is True |
| >= |
Greater than or equal to |
X>=8 to False |
| <= |
Less than or equal to |
X<=8 is True |
How to use
You can compare values by using comparison operators in conditional statements, and then act on the results:
if (age<18) document.write ("Too Young");
You will learn more about conditional statements in the next section of this tutorial.
logical operators
Logical operators are used to determine the logic between variables or values.
Given X=6 and y=3, the following table explains the logical operators:
| operator |
Description |
Example |
| && |
and |
(x < && y > 1) is True |
| || |
Or |
(x==5 | | y==5) to FALSE |
| ! |
Not |
! (x==y) is true |
Conditional operator
JavaScript also contains conditional operators that assign values to variables based on certain criteria.
Grammar
Variablename= (condition)? value1:value2
Example
Greeting= (visitor== "PRES")? " Dear President ":" Dear ";
If the value in the variable visitor is "PRES", assign "Dear President" to the variable greeting, otherwise the value "Dear" is assigned.