Operators in Dart
An expression is a special kind of statement that evaluates in a single value.
Every expression is composed of two parts:
Operand — They represent data.
Operator — An operator is a symbol that tells the compiler to perform a specific mathematical, relational, or logical operation and produce a final result.
For example, A + B where
Here A and B are operands and + is an operator.
Types of Operators in Dart
There are total of eight types of operators are there in Dart.
Arithmetic Operator
Assignment Operator
Bitwise operator
Equality Operator
Logical Operator
Relational Operator
Short circuit operator
Type Test Operator
Arithmetic Operator
There are nine types of arithmetic operators are there in a dart.
Operator | Meaning |
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
% | Modulo |
-expr | Unary Minus (reverse the sign of expression) |
~/ | Divide (Return Integer) |
++ | Increment Operator |
-- | Decrement Operator |
Relational Operator
The relational operator shows the relation between the two operands.
The relational operator can have only two values either true or false.
There are four relational operators are there.
Operator | Meaning |
> | Greater Than |
< | Less Than |
>= | Greater Than or Equal To |
<= | Less Than or Equal To |
Equality Operator
The equality operator checks whether two objects are equal or not.
Same as relational operators they contain Boolean values either true or false.
There are two equality operators are there.
Operator | Meaning |
\== | Equal To |
!= | Not Equal To |
- Here remember that two strings are equivalent only if they contain the same character sequence. Here the sequence of characters is important rather than the number of characters.
void main() {
var a = "Jay";
var b = "Jya"; //If here value is Jay then this is true
print(a == b);
}
Output
false
Type Test Operator
- Type test operators are used to check the data type.
Operator | Meaning |
is | True if the object has a specified type |
is! | True if the object has not specified type |
Assignment Operator
- Dart has two assignment operators.
Operator | Meaning |
\= | Assign value from right operand to left operand |
??= | Assign the value only if the variable is null |
- Here note that (+=, -=, *=, /=) are also a type of assignment operator. But as they are just a shortcut way. I didn’t mention them.
Logical Operator
Logical operators are used to combine two or more conditions.
Logical operators return a Boolean value of true or false.
There are three logical operators are there:
Operator | Meaning |
&& (AND) | Returns true if all conditions are true |
** | |
! (NOT) | Returns the inverse of the result |
Ternary operator
- Dart has two special operators which can help you to evaluate small expressions that might require an entire if-else statement.
Condition ? expr1 : expr2
- Here if the condition is true then the expr1 is evaluated and returns its value. Otherwise, expr2 is evaluated and returns the value.
void main() {
var a = 25;
var res = a > 15 ? "value greater than 15" : "value lesser than or equal to 15";
print(res);
}
Output
value greater than 15
expr1 ?? expr2
- Here if expr1 is non-null, return its value. Otherwise, evaluate and return the value of expr2.
void main() {
var a = 15;
var b = null;
var res = a ?? b;
print ( res );
}
Output
15
So, guys, that’s all you need to know about dart operators. Here short circuit and bitwise operators are also there. But as they are rarely used so I didn’t mention them here. But feel free to explore them if you need one. Also, feel free to let me know if I missed something. I’ll love to learn it from you.
Till Then Keep Loving, Keep Coding. And I’ll surely catch you up in another amazing article. Jai Hind, Vande Mataram 🇮🇳
Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.