Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values, and test for true or false.
For example, given that x = 5, the table below explains the comparison operators:
| Operator | Description | Comparing | Returns |
|---|---|---|---|
| == | equal to | x == 8 | false |
| x == 5 | true | ||
| x == "5" | true | ||
| === | equal value and equal type | x === 5 | true |
| x === "5" | false | ||
| != | not equal | x != 8 | true |
| !== | not equal value or not equal type | x !== 5 | false |
| x !== "5" | true | ||
| x !== 8 | true | ||
| > | 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 |
Oddities of Null
...
, 0 and undefined
| Equation | Returns |
|---|---|
| == |
| if (x ==1) | true if x = 1; |
| if (x ==1) | true if x = "1"; |
| if (x ==0) | true if x = 0; |
| if (x ==0) | true if x = null; |
| if (x ==0) | true if x = undefined; |
| === | |
|---|---|
| if (x ===1) | true if x = 1; |
| if (x ===1) | false if x = "'1"; |
| if (x ===0) | true if x = 0; |
| if (x ===0) | false if x = null; |
| if (x ===0) | false if x = undefined; |