Js comparison and introduction of logical operators. For more information about comparison and logical operators, see true or false.
Comparison Operators
Comparison operators are used in logical statements to determine whether variables or values are equal.
Given x = 5, the following table explains the comparison operators:
| Operator |
Description |
Example |
| = |
Equal |
X = 8 is false |
| === |
Full (Value and type) |
X = 5 is true; x = "5" is false |
| ! = |
Not equal |
X! = 8 is true |
| > |
Greater |
X> 8 is false. |
| < |
Less |
X <8 is true |
| > = |
Greater than or equal |
X> = 8 is false |
| <= |
Less than or equal |
X <= 8 is true |
How to Use
You can use a comparison operator in a condition statement to compare values and then take actions based on the results:
If (age <18) document. write ("Too young ");
In the next section of this tutorial, you will learn more about conditional statements.
Logical operators
Logical operators are used to determine the logic between variables or values.
Given x = 6 and y = 3, the following table describes the logical operators:
| Operator |
Description |
Example |
| && |
And |
(X <10 & y> 1) is true |
| | |
Or |
(X = 5 | y = 5) false |
| ! |
Not |
! (X = y) is true |
Conditional Operators
JavaScript also contains conditional operators that assign values to variables based on certain conditions.
Syntax
Variablename = (condition )? Value1: value2
Example
Greeting = (visitor = "PRES ")? "Dear President": "Dear ";
If the value in the variable visitor is "PRES", the value of "Dear President" is assigned to the variable greeting; otherwise, the value of "Dear" is assigned ".