Comparison Operators
So far, we have used two comparison operators, which are <
(less than) and >
(greater than). Now, let's list them all.
===
- Equals
!==
- Does not equal
>
- Greater than
>=
- Greater than or equal to
<
- Less than
<=
- Less than or equal to
Besides numbers, we can also compare text. For example:
const brand = "Toyota";
if (brand === "Toyota") {
console.log("The car brand is Toyota.");
}
else {
console.log("The car brand is not Toyota.");
}
// Prints: The car brand is Toyota.
Remember that all comparisons are ultimately true or false. If the comparison is true, the code in the if
is executed. If the comparison is false, the code in the else
is executed.
"Shakespeare" !== "Shakespeare" // False
10 === 10 // True
10 < 10 // False
10 >= 10 // True
Instructions
Create a variable brand
with a value of "Volkswagen"
.
Create an if
statement that prints Das Auto
if the variable brand
equals Volkswagen
.
Start programming for free
3/9