JavaScript ComparisonandLogical Operators
Comparison and logical operators are used for testingtrueorfalse。
Comparison Operators
Comparison operators are used in logical statements to determine whether variables or values are equal.
x=5The following table explains the comparison operators:
| Operator | Description | Comparison | Return value | Example |
|---|---|---|---|---|
| == | Equal to | x==8 | false | Example » |
| x==5 | true | Example » | ||
| === | Strict equal (value and type are equal) | x==="5" | false | Example » |
| x===5 | true | Example » | ||
| != | Not equal | x!=8 | true | Example » |
| !== | Strict not equal operator (either value or type is not equal, or both are not equal) | x!=="5" | true | Example » |
| x!==5 | false | Example » | ||
| > | Greater than | x>8 | false | Example » |
| < | Less than | x<8 | true | Example » |
| >= | Greater than or equal to | x>=8 | false | Example » |
| <= | Less than or equal to | x<=8 | true | Example » |
How to use
You can use comparison operators in conditional statements to compare values, and then take action based on the results:
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 < 10 && y > 1) is true |
| || | or | (x==5 || y==5) is false |
| ! | not | !(x==y) is true |
Conditional Operator
JavaScript also includes a conditional operator that assigns values to variables based on certain conditions.
Syntax
Example
Examples
If the value in the variable age is less than 18, assign "Too young" to the variable voteable, otherwise assign "Old enough".
Try it »