Relational operators are symbols used to compare values against one another. You’ve likely seen these plenty of times in basic math.
Relational Operator Reference Table
| Operator | Explanation |
|---|---|
== | Equal (checks if two values match) |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
The == operator also works with objects of all kinds.
Core Rules for the == Equality Operator
== checks whether two variables or objects hold identical values. If you need to strictly verify if two references point to the exact same object in memory, do not use == — use the dedicated identical() function instead.
Special Null Handling Rules If either side of the comparison is null:
- Returns
truewhen both operands arenull - Returns
falsewhen only one operand isnull
Logic for Regular Objects Under the hood, comparisons run the == method belonging to the left-hand operand, written conceptually as x.==(y). Every relational operator is just a member method attached to its operand.
The snippet below all output true — null never equals 0, nor does it match an empty string “”
void main() {
// Returns true when both sides are null
print(null == null); //true
// Returns true when one side is null and the other holds a value
print(null != 0); //true
print(null != ""); //true
}
Code language: PHP (php)
Here’s a full working example covering all relational operators:
void main() {
// Equality checks with ==
print(2 == 2);
print("dart" == "dart");
print([1,2] == [1,2]);
// Inequality checks with !=
print(2 != 3);
print(true != false);
// Greater than >
print(3 > 2);
print(10 > -5);
// Less than <
print(2 < 3);
print(-1 < 0);
// Greater than or equal >=
print(3 >= 3);
print(5 >= 2);
// Less than or equal <=
print(2 <= 3);
print(4 <= 4);
}
Code language: Dart (dart)
Program output from the code above:
true
true
false
true
true
true
true
true
true
true
true
true
trueCode language: JavaScript (javascript)
Key Difference Between == and identical()
void main() {
var a = [1, 2];
var b = [1, 2];
// The == operator compares List values; a and b are separate lists, so this outputs false
print(a == b);
// identical() checks for matching memory references; a and b are distinct objects, returns false
print(identical(a, b));
// c directly references a, pointing to the same object in memory, so identical() returns true
var c = a;
print(identical(a, c));
}
Code language: PHP (php)
Note: To compare the actual contents of two lists, use the listEquals utility function.
import 'package:flutter/foundation.dart';
listEquals(a, b); // Returns true if every element in both lists matches
Code language: JavaScript (javascript)
String Comparisons
Strings only support the == and != comparison operators.
void main() {
// Equality checks for strings
print("apple" != "banana"); //true
print("apple" == "apple"); //true
//print("zoo" > "apple"); // Throws an error: > < >= <= cannot be used with strings; only == and != work for matching
// Boolean equality checks
print(true == true); //true
print(true != false); //true
}
Code language: PHP (php)
Combining Relational & Arithmetic Operators
From earlier lessons, recall this precedence rule: arithmetic operations run before relational comparisons. The program will first resolve all addition, subtraction, multiplication and division, then compare the resulting values.
void main() {
// Calculations on each side run first, then the comparison
print(10 + 5 == 3 * 5); // 15 == 15 → true
print(20 - 3 != 8 + 9); // 17 != 17 → false
print(12 + 4 > 5 + 6); // 16 > 11 → true
print(9 - 7 < 10 - 1); // 2 < 9 → true
print(6 + 2 >= 4 * 2); // 8 >= 8 → true
print(15 - 8 <= 3 + 5); // 7 <= 8 → true
}Code language: PHP (php)
In all of these examples, Dart fully computes the math expressions on either side of the relational operator before comparing the final numbers.