Comparison Operators
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 15 of 77.
Comparison operators are used to compare two operands.
Sometimes we need to check whether an operand is bigger/smaller/... than another operand. The following table shows possible operators for comparison:
| Operator | Meaning | Example |
|---|---|---|
| == | Equal | 1 == 2 returns false |
| != | Not Equal | 1 != 2 returns true |
| > | Greater Than | 1 > 2 returns false |
| < | Lower Than | 1 < 2 returns true |
| >= | Greater or Equal | 1 >= 2 returns false |
| <= | Lower or Equal | 1 <= 2 returns true |
The comparison operator returns true if the comparison is correct or false otherwise.
For example:
let var1 = 13
let var2 = 12
let var3 = var1 != var2var3 will hold true because var1 and var2 are not equal
Remember the
booleantype,var3is a boolean.
Challenge
BeginnerWrite a script that initializes 2 variables n1 and n2 with the values 8 and 9 (accordingly).
After that initialize another variable n3 that will hold whether n1 is bigger than n2.
Cheat sheet
Comparison operators are used to compare two operands and return true or false:
| Operator | Meaning | Example |
|---|---|---|
| == | Equal | 1 == 2 returns false |
| != | Not Equal | 1 != 2 returns true |
| > | Greater Than | 1 > 2 returns false |
| < | Lower Than | 1 < 2 returns true |
| >= | Greater or Equal | 1 >= 2 returns false |
| <= | Lower or Equal | 1 <= 2 returns true |
Example usage:
let var1 = 13
let var2 = 12
let var3 = var1 != var2 // var3 holds trueTry it yourself
// Type your code below
// Don't change the line below
console.log(`n1 = ${n1}, n2 = ${n2}, n3 = ${n3}`)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False