JavaScript Comparison
- Previous Page JS Logic
- Next Page JS Condition
Comparison and logical operators are used to test 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 | Comparison | Return | Test |
---|---|---|---|---|
== | Equal to | x == 8 | false | Try It |
x == 5 | true | Try It | ||
x == "5" | true | Try It | ||
=== | Values are equal and types are equal | x === 5 | true | Try It |
x === "5" | false | Try It | ||
!= | Not equal | x != 8 | true | Try It |
!== | Values are not equal or types are not equal | x !== 5 | false | Try It |
x !== "5" | true | Try It | ||
x !== 8 | true | Try It | ||
> | Greater than | x > 8 | false | Try It |
< | Less than | x < 8 | true | Try It |
>= | Greater than or equal to | x >= 8 | false | Try It |
<= | Less than or equal to | x <= 8 | true | Try It |
How to use
Comparison operators can be used in conditional statements to compare values and take actions based on the results:
if (age < 18) text = "too young";
In the following chapters 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 explains the logical operators:
Operator | Description | Example | Test |
---|---|---|---|
&& | And | (x < 10 && y > 1) is true | Try It |
|| | Or | (x == 5 || y == 5) is false | Try It |
! | Not | !(x == y) is true | Try It |
Conditional (ternary) operator
JavaScript also includes a conditional operator that can assign a value to a variable based on certain conditions.
Syntax
variablename = (condition) ? value1:value2
Example
var voteable = (age < 18) ? "too young" : "sufficiently mature";
If the value of the variable age is less than 18, the value of the variable voteable will be "too young", otherwise the value of the variable voteable will be "sufficiently mature".
Comparing different types
Comparing different types of data may result in unpredictable outcomes.
If a string is compared with a number, JavaScript will convert the string to a number when making the comparison. An empty string will be converted to 0. A non-numeric string will be converted to always be false
of NaN
.
Case | Value | Test |
---|---|---|
2 < 12 | true | Try It |
2 < "12" | true | Try It |
2 < "Bill" | false | Try It |
2 > "Bill" | false | Try It |
2 == "Bill" | false | Try It |
"2" < "12" | false | Try It |
"2" > "12" | true | Try It |
"2" == "12" | false | Try It |
When comparing two strings, "2" is greater than "12" because (in alphabetical order) 1 is less than 2.
To ensure correct results, the variable should be converted to the appropriate type before comparison:
age = Number(age); if (isNaN(age)) { voteable = "Input Error"; } voteable = (age < 18) ? "Too Young" : "Sufficiently Mature"; }
- Previous Page JS Logic
- Next Page JS Condition